Programming ruby-Regular Expression

Source: Internet
Author: User

Regular Expression

Most of Ruby's built-in types and otherProgramming LanguageVery similar. Mainly include strings, integers, floats, and arrays. However, only the script language,

Such as Ruby, Perl, and awk provide support for built-in expression types. It is a powerful text processing tool, though the regular expression is relatively hidden.

They are much different from simply adding interface class libraries.

Regular Expressions are a simple method to match strings using the specified pattern. In Ruby, a typical way to create a regular expression is to write the pattern between two diagonal lines (/pattern /).

After all, Ruby is Ruby, and regular expressions are also objects and can operate like objects.

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

/Perl | Python/

In the body of a forward slash, there are two strings to be matched. They are separated by (|. The pipeline operator indicates "the Left or Right". In this mode, it is Perl or Python.

You can also use parentheses in the pattern, as in arithmetic expressions, so this pattern can also be written

/P (ERL | ython )/

You can also specify duplicates in the mode. /AB + C/match a string with one or more bits after a and then a C. Replace "+" with "*". The/AB * C/regular expression is

Match A 'A' followed by 0 or more 'B' followed by a C.

You can also match a group of characters in the mode. Examples of common character types include \ s, which matches a blank character (space, tab, line break, etc.); \ D matches any number;

\ W matches any typical word character. Period (.) match (basically) any character.

We combine all these to form a practical regular expression.

/\ D: \ D/# a time such as 12:34:56
/Perl. * Python/# Perl, zero or more other chars, then Python
/Perl Python/# Perl, a space, and Python
/Perl * Python/# Perl, zero or more spaces, and Python
/Perl + Python/# Perl, one or more spaces, and Python
/Perl \ s + Python/# Perl, whitespace characters, then Python
/Ruby (Perl | Python)/# Ruby, a space, and either Perl or Python

Once a mode is created, it is very depressing not to use it. Matching operator = ~ It is used to match a string with a regular expression. If the match is successful,

= ~ Returns the location where the first match is successful. Otherwise, it returns nil. That is to say, you can use a regular expression in the condition declaration of IF and while. For exampleCodeFragment,

If the string contains text Perl or Python, output a message.

If line = ~ /Perl | Python/
Puts "scripting language mentioned: # {Line }"
End

The ruby replacement method can replace some text matching regular expressions in a string with other text.

Line. sub (/perl/, 'Ruby ') # Replace first 'perl' with 'Ruby'
Line. gsub (/Python/, 'Ruby ') # replace every 'python' with 'Ruby'

You can use Ruby to replace all the places where Perl and Python appear.
Line. gsub (/perl | Python/, 'Ruby ')

The code for combining the above knowledge is as follows:

Line = 'I like python program. It is a good Lanuage.
I havent learned about Perl. Do you know Perl? '
If line = ~ /Perl | Python/
Puts "scripting language mentioned: # {Line }"
End
Puts "Run line. sub (/perl/, 'Ruby ')"
Puts line. sub (/perl/, 'Ruby ')
Puts "Run line. gsub (/Python/, 'Ruby ')"
Puts line. gsub (/Python/, 'Ruby ')
Puts "Run line. gsub (/perl | Python/, 'Ruby ')"
Puts line. gsub (/perl | Python/, 'Ruby ')

The output result is as follows:

Scripting language mentioned: I like python program. It is a good Lanuage.

I havent learned about Perl. Do you know Perl?

Run line. sub (/perl/, 'Ruby ')

I like python program. It is a good Lanuage.

I havent learned about Ruby. Do you know Perl?

Run line. gsub (/Python/, 'Ruby ')

I like Ruby program. It is a good Lanuage.

I havent learned about Perl. Do you know Perl?

Run line. gsub (/perl | Python/, 'Ruby ')

I like Ruby program. It is a good Lanuage.

I havent learned about Ruby. Do you know 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.