1.Create Regular Expression
A) reg1 =/^ [a-z] * $/# Place the schema definition between two forward slashes and return a Regexp object
B) reg2 = Regexp. new ('^ [a-z] * $') # create a Regexp object
C) reg3 = % r {^ [a-z] * $} # Use the frontend % r
2. Match the Regular Expression: Both String and Regexp support the following two methods:
A) match Method: If the match succeeds, an instance of the MatchData class is returned; otherwise, nil is returned;
B)= ~Operator: If the match is successful, an index (integer) is returned. Otherwise, nil is returned;
Example:
Puts (/abc/= ~ 'Abc') # => return 0
Puts (/abc/= ~ 'Cdg') # => return nil
Puts (/abc/. match ('abc') # => return abc
Puts (/abc/. match ('cdg') # => return nil
3.Matching Group
In a Ruby regular expression, you can use a regular expression to match one or more sub-strings.
The expression is enclosed in parentheses. You can use the substring specified in parentheses to save the matched string. The following regular expression contains two groups (hi) and (h... O ):
/(Hi). * (h... o)/= ~ "The word 'Hi' is short for 'Hello '."
When the matching is successful, the matching value is assigned to some variables (the number of groups in the regular expression is variable). These variables can be passed through $1, $2, $3... If you execute the above line of code, you can use $1, $2 to access the variable:
Print ($1, "", $2, "\ n") # => hi hello
Note: If the entire regular expression match fails, no variable will be initialized, but nil will be returned.
4.MatchDataType
As mentioned above, use = ~ The return value is an integer or nil. When the match method is used, the MatchData object is returned, which contains the matching results. At first glance, it is like a string:
Puts (/cde/. match ('abcdefg') # => cde
Puts (/cde/= ~ ('Abcdefg') # => cde # => 2
In fact, it is an instance of the MatchData class and contains a string:
P (/cde/. match ('abcdefg') #=># <MatchData: "cde">
You can use the to_a or captures method of the MatchData object to return an array containing its values:
X =/(^. *) (#) (. *)/. match ('def myMethod # This is a very nice method ')
X. captures. each {| item | puts (item )}
The above code will output:
Def myMethod
#
This is a very nice method
Note: The captures and to_a methods are slightly different. The latter will contain the original string
X. captures # => ["def myMethod", "#", "This is a very nice method"]
X. to_a # => ["def myMethod # This is a very nice method", "def myMethod", "#", "This is a very nice method"]
5.Pre & PostMethod
A) pre_match or ($ '): returns the string before the matching string.
B) post_match or ($ '): returns the string after the matching string.
X = // #/. match ('def myMethod # This is a very nice method ')
Puts (x. pre_match) # => def myMethod
Puts (x. post_match) # => This is a very nice method
6.Greedy match
When a string contains multiple possible matches, sometimes only the first matching string may be returned;
Sometimes you may want to return all matched strings. In this case, greedy matching is called. The symbols * (0 or more) and + (1 or more) can be used for greedy matching. Use Symbols? (0 or 1) for minimum matching;
Puts (/. * at/. match ('The cat sat on The mat! ') # => Returns: The cat sat on the mat
Puts (/.*? At/. match ('The cat sat on The mat! ') # => Returns: The cat
7.Method In string
A) = ~ And match: The same as Regexp.
B) String. scan (pattern): perform as many matches as possible and add the first match to the array.
TESTSTR = "abc is not CBA"
B =/[abc]/. match (TESTSTR) # => MatchData: "a" puts ("-- scan --")
A = TESTSTR. scan (/[abc]/) # => Array: ["a", "B", "c", "c", "B", "a"]
In addition, you can pass a block to the sacn method:
A = TESTSTR. scan (/[abc]/) {| c | print (c. upcase) }#=> ABCCBA
C) String. split (pattern): splits the original String based on pattern and returns an array. If pattern is null (//), splits the original String into characters;
S = "def myMethod # a comment"
P (s. split (/m. * d/) # => ["def", "# a comment"]
P (s. split (/\ s/) # => ["def", "myMethod", "#", "a", "comment"]
P (s. split (//) # => ["d", "e", "f", "", "m", "y", "M ", "e", "t", "h", "o", "d", "", "#", "", "","", "c", "o", "m", "m", "e", "n", "t"]
D) String. slice (pattern): returns the matched String (the original String remains unchanged ),
String. Slice! (Pattern): returns a matched string and deletes the matched string from the original string (modifies the value of the original string)
S = "def myMethod # a comment"
Puts (s. slice (/m. * d/) # => myMethod
Puts (s) # => def myMethod # a comment
Puts (s. slice! (/M. * d/) # => myMethod
Puts (s) # => def # a comment
8. Regular Expression matching rules
Rules |
Description |
// |
Match character |
/\? / |
Match special characters ?. Special characters include ^, $ ,? ,.,/, \, [,], {,}, (,), + ,*. |
. |
Match any character, such as/a./match AB and ac. |
/[AB] c/ |
Match the range between ac and bc, [], for example:/[a-z]/,/[a-zA-Z0-9]/. |
/[^ A-zA-Z0-9]/ |
Match strings not in this range |
/[\ D]/ |
Represents any number |
/[\ W]/ |
Represents any letter, number, or _ |
/[\ S]/ |
White space characters, including spaces, tabs, and line breaks |
/[\ D]/,/[\ W]/,/[\ S]/ |
All are the above negative conditions. |
? |
0 or 1 Character |
* |
Represents 0 or multiple characters |
+ |
1 or more characters |
/D {3 }/ |
Match 3 Numbers |
/D {1, 10 }/ |
Match 1-10 numbers |
D {3 ,}/ |
Match more than three numbers |
/([A-Z] \ d) {5 }/ |
Match strings with uppercase letters and digits. |