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”.

目录
相关文章
|
11月前
|
搜索推荐 索引
Term Suggester 中 suggest_mode 的三种模式missing、popular、always 的区别
Term Suggester 中 suggest_mode 的三种模式missing、popular、always 的区别
|
Java Unix Shell
Regular Expressions (9)
基于POSIX BRE & ERE
141 0
Regular Expressions (9)
|
JSON JavaScript 前端开发
Angular Template expression operators介绍
Angular Template expression operators介绍
Angular应用ng build的一些边界情况boundary condition
Angular应用ng build的一些边界情况boundary condition
112 0
Angular应用ng build的一些边界情况boundary condition
OPA 14 - search existing item by regular expression
Created by Wang, Jerry, last modified on Nov 08, 2015
107 0
OPA 14 - search existing item by regular expression
|
存储 测试技术 C++
use regular expression instead of ABAP function module to parse attachment
在做my task offline performanc improvement时,先参考BP代码,里面有一行call 下图的FM去将变量ls_key里存储的attachment information解析出来:
111 0
use regular expression instead of ABAP function module to parse attachment