Basic guidelines for using regular expressions in Ruby _ruby topics

Source: Internet
Author: User
Tags character classes

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:
/perl| python/

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:
/p (Erl|ython)/

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 ')

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.