Group, Greedy and Lazy in Regular Expression

简介:   (…) means a group, group can have a name, form is: (?’groupname’…) or (?…) each group has a GroupID (from 1 to infinite),...

 

(…) means a group, group can have a name, form is: (?’groupname’…) or (?<groupname>…)

each group has a GroupID (from 1 to infinite), group 0 is the whole matched string

If you want to reference group, you may use /k<groupname> or /GroupNumber

 

For example:

Input: “dream is a dream”

Pattern: “(dream) is a /1”

Result: matched

The first “dream” is group 1, group 0 is “dream is a dream”

 

Input: “dream is a dream”

Pattern: “(?<drm>dream) is a /k<drm>”

Result: matched

Group “drm” is dream, /k<drm> reference group “drm”

 

“Greedy” means a pattern will match a string as long as possible, it’s by default, for example:

Input: dreamdream

Pattern: (dream)*

Result: matched

Matched value: dreamdream

 

 

We can set an lazy pattern:

 

Input: dreamdream

Pattern: (dream)*?

Result: matched

Matched value: Empty

Because *? means as less as possible, 0 is the minimum, so it’s empty

 

Input: dreamdream

Pattern: (dream)+?

Result: matched

Matched value: dream

Because +? means as less as possible, 1 is the minimum, so it’s “dream”, if you use the function “Matches”, you will get 2 Match object, they both are “dream”.

相关文章
|
8月前
|
Regular Expression
【7月更文挑战第1天】
48 1
正则表达式 Regular Expression
正则表达式 Regular Expression
58 0
Term Suggester 中 suggest_mode 的三种模式missing、popular、always 的区别
Term Suggester 中 suggest_mode 的三种模式missing、popular、always 的区别
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等