(…) 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”.