Regular expressions-four ways to use the question mark

Source: Internet
Author: User
Regular Expressions-four ways to use the question mark Original Symbol

Because there are special meanings in regular expressions, so if you want to match, you need to escape, \? There are no quantifiers

A question mark can mean repeating the previous 0 or one time, which is either not appearing or appearing once. non-greedy match Greedy Match

Matches the string as long as possible when matching matches, by default, greedy matching

String pattern1 = @ "A.*c";   
Regex regex = new Regex (PATTERN1);
Regex. Match ("Abcabc"); Return "ABCABC"
non-greedy match

Matches the shortest possible string, using the? To represent a non-greedy match when the match is met

String pattern1 = @ "A.*?c";   Non-greedy match 
regex regex = new Regex (PATTERN1);
Regex. Match ("Abcabc"); Return "ABC"
several commonly used non-greedy matching pattern*? Repeat any number of times, but with as few repetitions as possible +? Repeat 1 or more times, but repeat as little as possible?? Repeat 0 or 1 times, but repeat {n,m} as little as possible. Repeat N to M times, but repeat {n,} as little as possible. Repeat more than n times, but repeat as little as possible do not capture mode

How to turn off the ability to capture parentheses. Instead of just using it for grouping, the method is to add the following opening parenthesis:?, where the first parenthesis is only used for grouping, not capturing the capture variable, so the contents can only be steak or burger, and never be bronto.
while (<>) {
if (/(?: Bronto) (Steak|burger)/) {
Print "Fred wants a $1\n";
}
}



(pattern)
Matches the pattern and captures the matched sub-expression. You can use the $0...$9 property to retrieve a captured match from the result "match" collection. To match the bracket character (), use "\ (" or "\)".

(?:p Attern)
A subexpression that matches the pattern but does not capture the match, that is, it is a non-capturing match and does not store a match for later use. This is useful for combining pattern parts with the or character (|). For example, "Industr (?: y| ies)" is a more economical expression than "industry|industries".

(? =pattern)
A subexpression that performs a forward lookahead search that matches the string at the starting point of the string that matches the pattern. It is a non-capture match, that is, a match that cannot be captured for later use. For example, "Windows (? =95| 98| Nt| 2000) matches "Windows" in Windows 2000, but does not match Windows 3.1 in Windows. Lookahead does not occupy characters, that is, when a match occurs, the next matching search immediately follows the previous match, rather than the word specifier that makes up the lookahead.

(?! Pattern
A subexpression that performs a reverse lookahead search that matches a search string that is not at the starting point of a string that matches the pattern. It is a non-capture match, that is, a match that cannot be captured for later use. For example, "Windows (?! 95| 98| Nt| 2000) matches "Windows" in Windows 3.1, but does not match Windows 2000 in Windows. Lookahead does not occupy characters, that is, when a match occurs, the next matching search immediately follows the previous match, rather than the word specifier that makes up the lookahead.

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.