Regular (? =) (? :)

Test website:
https://regex101.com/r/xHeJSZ/3/

正则表达式 - (?!), (?:), (?=)
(?:pattern) 
非获取匹配,匹配pattern但不获取匹配结果,不进行存储供以后使用。
这在使用或字符“(|)”来组合一个模式的各个部分是很有用。
例如“industr(?:y|ies)”就是一个比“industry|industries”
更简略的表达式。
(?=pattern)
非获取匹配,正向肯定预查,在任何匹配pattern的字符串开始
处匹配查找字符串,该匹配不需要获取供以后使用。例如,
“Windows(?=95|98|NT|2000)”能匹配“Windows2000”
中的“Windows”,但不能匹配“Windows3.1”中的“Windows”。
预查不消耗字符,也就是说,在一个匹配发生后,在最后一次
匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字
符之后开始。
(?!pattern)
非获取匹配,正向否定预查,在任何不匹配pattern的字符串
开始处匹配查找字符串,该匹配不需要获取供以后使用。例如
“Windows(?!95|98|NT|2000)”能匹配“Windows3.1”中
的“Windows”,但不能匹配“Windows2000”中的“Windows”。

    
    
    
eg:
正则:in(.*?)(?:(?=stry)|srtddd|\;)
要匹配的字串:indutryd indusrtdd induaa
结果:(indu后面的只匹配不占字符)
Match 1
Full match	0-4	indu
Group 1.	2-4	du   //匹配一
Match 2
Full match	9-19	indusrtddd
Group 1.	11-13	du   //匹配2
    
eg:
正则:indu(.*?)(?:(?=[i])|$)
要匹配的串 : indutryd indusrtdd induaa
Match 1
Full match	0-9	indutryd 
Group 1.	4-9	tryd 
Match 2
Full match	9-19	indusrtdd 
Group 1.	13-19	srtdd 
Match 3
Full match	19-25	induaa
Group 1.	23-25	aa

There are examples above!
The following is a single example to pay attention to the difference between FULL Match!

eg:(?:)就等同于indu()yd|indu()tdd|indu()aa

indu(.*?)(?:yd|tdd|aa)
    
TEST STRING:
indutryd indusrtdd induaa
    
RESULT:
Match 1
Full match	0-8	indutryd
Group 1.	4-6	tr
Match 2
Full match	9-18	indusrtdd
Group 1.	13-15	sr
Match 3
Full match	19-25	induaa
Group 1.	23-23	
eg:indu(.*?)(?=yd|tdd|aa) 正向预查 可以看到fullmatch有区别的
TEST STRING:
indutryd indusrtdd induaa
    
Result:
Match 1
Full match	0-6	indutr
Group 1.	4-6	tr
Match 2
Full match	9-15	indusr
Group 1.	13-15	sr
Match 3
Full match	19-23	indu
Group 1.	23-23	

Published 27 original articles · praised 1 · visits 1673

Guess you like

Origin blog.csdn.net/qq_37959151/article/details/105185268