The built-in support for regular expressions is usually limited to scripting languages such as Ruby,perl and awk, which is a disgrace: Although regular expressions are mysterious, they are a powerful tool for text processing. It is very different to support it through the built-in rather than through the library interface.
A regular expression is simply a method of specifying a character pattern that matches in a string. In Ruby, you typically write patterns (pattern) between slashes (/pattern/) to create regular expressions. At the same time, Ruby is Ruby, the regular expression is an object and can be manipulated as an object.
For example, you can use a regular expression like the following to write a pattern that matches a string containing Perl or Python.
Copy Code code as follows:
The preceding slash defines the pattern, which consists of two substrings to match, separated by a pipe character (|). A pipe character means "either the right string, or the left string." In this case, they are Perl or Python, respectively. As in an arithmetic expression, parentheses can be used in a pattern, so you can write the pattern as
Copy Code code as follows:
You can also specify a duplicate (repetition) in the pattern. /ab+c/matches a string containing a, followed by one or more B, followed by C. To change the plus sign in the pattern to an asterisk,/ab*c/creates a regular expression that matches a, 0, or more B and then C.
You can also match one or more groups of characters in a pattern. Some common examples are character classes (character classes) such as \s, which match white space characters (spaces, tabs, carriage return line breaks, and so on), \d match any number, and \w, which matches any character that appears within a word. A point (.) matches almost any character.
Once the pattern is created, it's always embarrassing not to use it. The =~ matching operator can match a string with a regular expression. If a pattern is found in the string, =~ returns the start of the pattern, otherwise it returns nil. This means that a regular expression can be used as a condition in an if and a while statement. For example, if the string contains Perl or Python, the following code prints a message.
Copy Code code as follows:
If line=~/perl| python/
Puts "scripting language Mentioned:#{line}"
End
The part of the string that a regular expression matches to, which can be replaced with another text using one of Ruby's substitution methods.
Copy Code code as follows:
Line.sub (/perl/, ' Ruby ') #用 ' Ruby ' replaces the first ' Perl '
Line.gsub (/python/, ' Ruby ') #用 ' Ruby ' replaces all ' Python '
Use the following statement to replace every place in Perl and Python with Ruby.
Copy Code code as follows:
Line.gsub (/perl| python/, ' Ruby ')