Basic instructions for using regular expressions in Ruby, ruby Regular Expressions

Source: Internet
Author: User
Tags character classes

Basic instructions for using regular expressions in Ruby, ruby Regular Expressions

Built-in support for regular expressions is usually limited to scripting languages such as Ruby, Perl, and awk. This is a shame: although regular expressions are mysterious, they are a powerful text processing tool. It is quite different to support it through built-in instead of library interfaces.

A regular expression is only a method that specifies the character pattern. This character pattern is matched in the string. In Ruby, pattern is usually written between diagonal lines (/pattern/) to create a regular expression. At the same time, Ruby is Ruby, and regular expressions are objects and can be operated as objects.

For example, you can use the following regular expression to write a pattern. It matches a string containing Perl or Python.

Copy codeThe Code is as follows:/Perl | Python/

The forward slash defines this pattern. The pattern consists of two substrings to be matched, which are separated by the pipeline operator (|. The pipeline character means "either the string on the right or the string on the left ". In this example, they are Perl or Python. As in arithmetic expressions, parentheses can be used in the pattern, so the pattern can be written

Copy codeThe Code is as follows:/P (erl | ython )/

You can also specify repetition in the mode ). /AB + c/matches a string that contains a, followed by one or more B, followed by c. Change the plus sign in the mode to an asterisk./AB * c/creates a regular expression that matches a, zero, or multiple B and then c.

You can also match one or more characters in the mode. Some common examples are character classes (character classes) such as \ s, which match blank characters (space character, Tab character, carriage return newline character, etc.); \ d matches any number; and \ w, it matches any character that appears in a word. A point (.) matches almost any character.

Once a mode is created, it is always embarrassing not to use it. = ~ Matching operators can use regular expressions to match strings. If the mode is found in the string ~ Returns the starting position of the mode. Otherwise, it returns nil. This means that the regular expression can be used as a condition in the if and while statements. For example, if a string contains Perl or Python, the following code outputs a message.

Copy codeThe Code is as follows: if line = ~ /Perl | Python/

Puts "Scripting language mentioned: # {line }"

End

To replace the string that matches the regular expression with other text, use one of Ruby's replacement methods.

Copy codeThe Code is as follows: line. sub (/Perl/, 'Ruby ') # Replace the first 'perl' with 'Ruby'

Line. gsub (/Python/, 'Ruby ') # Use 'Ruby' to replace all 'python'

Use the following statement to replace each part of Perl and Python with Ruby.

Copy codeThe Code is 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.