Introduction to regular expressions and pattern matching (3)

Source: Internet
Author: User
Tags expression engine
Introduction to regular expressions and pattern matching-annotation and Pattern Transformation

Author: Zhang guiquan

(From 《

Regular Expression pocket manual. Translator, Zhang guiquan. You can download the translated version from http://download.csdn.net/source/410896. If it feels good, buy an original book, regular expression pocket reference, and support the author of the original book. Reading PDF files is also tiring. Don't forget to give me some valuable comments. Thank you. Ajax.mailer@gmail.com)

The pattern change changes the way the Regular Expression Engine interprets the regular expression. (See MRE 110-113,135-136)

 

Multiline mode:M

Change the behavior of ^ and $ to match the line break next to the input string.

 

Single Row mode:S

Changes the dot behavior to match all characters, including line breaks in the input string.

 

Case Sensitive Mode:I

Use uppercase or lowercase letters to uniquely distinguish between letters.

 

 

Adjust the interval freely (Free-spacing) Mode:X

Regular Expressions are allowed to contain spaces and comments. Spaces and comments (starting with # and ending with the end of the line) are ignored by the Regular Expression Engine.

 

Mode Converter :(?!),(? -!),(? MoD:...)

In general, you can use (? MoD) sets the mode converter in the regular expression, which takes effect in subsequent subexpressions. (? -Mod) cancels the mode in subsequent subexpressions. (? MoD:...) Start or cancel the mode between comma and parentheses. For example (? : Perl) matches with Perl, uses Perl, and uses Perl.

 

Note :(? #...) And #

In the free interval adjustment mode, # indicates that this is a line of comment. When # is not supported, you can set (? #...) Embedded in any location of the regular expression, No matter what mode. For example,. {0, 80 }(? # Field limit is 80) allows you to annotate your own rewrite reasons. {0, 80 }.

 

Text Span one by one (Literal-text Span):/Q...\E

Avoid metacharacters between/Q and/e. For example,/Q (. *)/E is equivalent.

 

Grouping, capturing, condition and Control

This section includes the grouping sub-mode, capturing sub-match, the syntax of condition sub-match, and the number of times that the computing sub-mode match appears. (See MRE 137-142)

 

Capturing and categorizing parentheses (...) AND/1,/2And so on

Brackets play two roles: Classification and capture. The text that matches the quilt pattern in brackets will be used later. Calculate the number of square brackets from left to right to obtain the number of square brackets. If backward reference is allowed, you can use/1,/2, and so on to reference A submatch in the same match. For text capturing, You can implement this mode by implementing the specified method. For example, if/B (/W +)/B/S +/1 matches duplicate words, it is not as good as.

 

Only parentheses for classification :(? :...)

A sub-regular expression is categorized for conversion or metering, rather than capturing a sub-matching. This is useful because of its efficiency and high reusability. For example ,(? : Foobar) matches foobar, but does not save it to a capture group.

 

Naming Capture :(? <Name>...)

Capture and classify, and then use name to reference the captured text. For example, subject :(? <Subject>. *) capture the text after the subject, capture and classify the text, and reference the text through the subject.

 

Automatic Classification: (?> ...)

Even if the match fails, no matching text in the group will be traced back. For example [AB] *)/W matches with aabbcc, and the burden is not aabbaa.

 

 

Replace :... |...

Several subexpressions can be tested. Replacement with a lower priority may sometimes lead to a much larger subexpression than expected, so it is best to use brackets to specify the content you want to replace. For example,/B (FOO | bar)/B matches Foo or bar.

 

Condition :(?(If)Then|Else)

IfDepends on the specific implementation, but it is usually used to capture the subexpression or look around (lookaround) reference. WhileThenAndElseAll are in regular expression mode. WhenIfWhen the condition is true, referenceThenOtherwise, useElse. For example, (<)? Foo (? (1) | (bar) matches Foo and foobar.

 

Greedy gauge :*, +,?, {Num, num}

Use the greedy gauge to check how many times a structure can be applied. All the matches are attempted, but if the match is successful, you can trace back or discard the match. For example, (AB) + matches all abababababab.

 

Lazy quantifiers):*?, +?,??, {Num, num }?

The inertia gauge controls the number of times a structure may be applied. But unlike the greedy gauge, it tries to perform as few matching times as possible. For example, (an) +? Only match banana once.

 

Possessive quantifiers): * +, ++,? +, {Num, num} +

All the metering devices are like greedy metering devices, but the lock ("lock in") does not allow backtracing of submatching. For example, (AB) ++ AB does not match abababababab.

 

Unicode support

The Unicode Character Set assigns a unique number to each character in all languages in the world. Because the number of characters may be too large, Unicode requires more than one byte to represent a character. Some regular expressions do not support Unicode characters because they require 1-byte ASCII characters. Basic Support for Unicode characters, starting from matching a unicode string by word. Advanced Support includes character sets and ing of all other structures that support Unicode languages. For example,/W may match e either.

 

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.