Programming ruby--Regular Expression

Source: Internet
Author: User
Tags regular expression

Most of Ruby's built-in types are similar to other programming languages. Mainly have strings,integers,floats,arrays and so on. However, only the scripting language,

such as Ruby,perl, and awk provide support for built-in expression types. Shame: Regular expressions are a powerful text-processing tool, albeit covert.

They are very different than simply adding an interface class library.

A regular expression is an easy way to match a string with a specified pattern. In Ruby, the typical way to create a regular expression is to write the pattern between two slashes (/pattern/).

After all, Ruby is Ruby, and regular expressions are objects and can be manipulated like objects.

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

/perl| python/

In the forward slash body, there are two strings that we want to match, separated by (|). This pipe character means "left or right", which is Perl or Python in this pattern.

You can also use parentheses in a pattern, just as you would in an arithmetic expression, so this pattern can also be written as

/p (Erl|ython)/

You can also specify duplicates in the pattern. /ab+c/matches a string with one or more b followed by a C. Replace the + number with the * number, the regular expression created by/ab*c/is

Match a A followed by 0 or more b followed by a C.

You can also match a set of characters in a pattern. The commonly used character type example has \s, it matches a white space character (Space,tab, line break, etc.); \d matches any number;

\w matches any of the typical word characters. a period (.) matches (essentially) any character.

We combine all of these together to make a useful regular expression.

/\d\d:\d\d:\d\d/# A time such as 12:34:56
/perl.*python/# Perl, zero or more other chars, then Python
/perl Pyth on/# Perl, a spaces, 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

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.