Ruby Regular Expression tutorial

Source: Internet
Author: User
Tags chop uppercase letter

Let's look at a more interesting program. This time we will test whether a string matches the description produced by concise pattern encoding.

In these pattern modes, some characters or character combinations have unique meanings, including:

Copy codeThe Code is as follows: [] range Descriptor (for example, [a-z] indicates a letter in the range of a to z)

\ W letters or numbers; equivalent to [0-9A-Za-z]

\ W non-letters, numbers

\ S [\ t \ n \ r \ f] is a null character, which is equivalent to [\ t \ n \ r \ f]

\ S non-null characters

\ D [0-9] Number; equivalent to [0-9]

\ D non-numeric characters

\ B Return character (0x08) (only within the range descriptor)

\ B word boundary (when the range descriptor is external)

\ B Non-word boundary

* The first element appears 0 or multiple times.

+ The first element appears one or more times.

The first element of {m, n} appears at least m times and at most n times

? The preceding element can appear at most once; equivalent to {0, 1}

| Match with the expression above or below

() Group)

The odd words used in those patterns are called regular expressions. like Perl, Ruby also uses forward slashes (instead of double quotation marks) to enclose them. if you have never used regular expressions before, maybe they seem to be nothing except Rules (regular), but it is wise to spend a little time learning about them. when you need to perform pattern matching, search, or other operations on strings, its efficient expression can cure your headache (and save a lot of line of code ).

For example, suppose we want to test whether a string matches such a description "starting with lowercase f, followed by an uppercase letter, and possibly followed by many non-lowercase letters. "If you are a sophisticated C programmer, your mind may be filled with dozens of programs, right? Admit it, you cannot control yourself. in Ruby, you just need to use your string with a regular expression/^ f [A-Z] (^ [a-z]) * $/to check it.

What about "a 16-digit number enclosed by <> "? No problem.

Copy codeThe Code is as follows: ruby> def chab (s) # "contains hex in angle brackets"
| (S = ~ /<0 (x | X) (\ d | [a-f] | [A-F]) +> /)! = Nil
| End
Nil
Ruby> chab "Not this one ."
False
Ruby> chab "Maybe this? {0x35} "# wrong kind of brackets
False
Ruby> chab "Or this? <0x38z7e> "# bogus hex digit
False
Ruby> chab "Okay, this: <0xfc0004> ."
True

Although regular expressions seem a headache at the beginning, you will soon be satisfied with the ability to express your meaning so efficiently.

Below is a small program that can help you experiment with regular expressions, save it as regx. rb, and then input 'Ruby regx. rb' in the command line to run the program.

Copy codeThe Code is as follows: # Requires an ANSI terminal!
St = "\ 033 [7 m"
En = "\ 033 [m"
While TRUE
Print "str>"
STDOUT. flush
Str = gets
Break if not str
Str. chop!
Print "pat>"
STDOUT. flush
Re = gets
Break if not re
Re. chop!
Str. gsub! Re, "# {st }\\ {en }"
Print str, "\ n"
End
Print "\ n"

This applet requires two inputs, one string and one regular expression. the input string is verified by a regular expression, and then all matching parts are displayed with the brightness of the reverse video. leave the details alone, and you will have code analysis later.

Copy codeCode: str> foobar
Pat> ^ fo +
Foobar
~~~

The red part above is displayed as a reverse view in the program input "~~~ "Rows are used to facilitate the use of character-based browsers.

Let's try again:

Str> abc012dbcd555
Pat> \ d
Abc012dbcd555

If you are surprised, look at the table at the beginning of this page: \ d has nothing to do with the letter d, but corresponds to a single number.

What if there are more than one method that can match the pattern?

Str> foozboozer
Pat> f. * z
Foozboozer
~~~~~~~~

Foozbooz is matched, not just fooz, because a regular expression matches the longest substring as much as possible.

The following is a pattern matching that isolates the number time periods separated by colons from strings.

Str> Wed Feb 7 08:58:04 JST 1996
Pat> [0-9] +: [0-9] + (: [0-9] + )?
Wed Feb 7 08:58:04 JST 1996

"= ~ "Is a matching operator used to match regular expressions. It returns the matching position found in the string or the nil expression indicating that the pattern cannot match.

Ruby> "abcdef" = ~ /D/
3
Ruby> "aaaaaa" = ~ /D/
Nil

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.