Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What's the use of regular expression question marks?

Shulou Source: shulou.com Published: 2022-06-02 03:47:03 10月03日 Update

This article mainly introduces the regular expression question mark what is the use, the article is very detailed, has a certain reference value, interested friends must read it!

Original text symbol

Because? There is a special meaning in regular expressions, so if you want to match? Itself, it needs to be escaped.

With or without quantifier

The question mark can indicate that the previous content is repeated 0 times or once, that is, it either does not appear or occurs once.

Non-greedy matching

Greedy matching

When a match is satisfied, match the string as long as possible. By default, greedy matching is used.

String pattern1 = @ "a.progresc"; / / greedy match Regex regex = new Regex (pattern1); regex.Match ("abcabc"); / / return "abcabc"

Non-greedy matching

When satisfying the match, match the string as short as possible, using? To represent non-greedy matching

String pattern1 = @ "a.roomroomc"; / / 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 repeat as little as possible

+? Repeat one or more times, but repeat as little as possible

?? Repeat 0 or 1 times, but repeat as little as possible

{n,m}? Repeat n to m times, but repeat as little as possible

{n,}? Repeat more than n times, but repeat as little as possible

Do not snap mode

How do I turn off the ability to capture parentheses? Instead, you just use it for grouping by adding:? after the left parenthesis, where the first parenthesis is only used for grouping and does not occupy captured variables, so the content of $1 can only be steak or burger, but can never be bronto.

While () {if (/ (?: bronto) (steak | burger) /) {print "Fred wants a $1\ n";}} above is all the content of the article "what's the use of regular expression question marks". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: As much as possible greed content rules expressions question marks just characters strings more articles or grouping special that is value interest original text variables meaning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Docker Xiaomi MariaDB Redmi