8

正则表达式的零宽断言

 3 years ago
source link: https://blogread.cn/it/article/5833?f=hot1
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

正则表达式的零宽断言

浏览:1807次  出处信息

平时一直都在用非常简单的表达式,匹配位置的时候,用几个元字符就够了。

当今天不得不从别人的C源代码中取出一个特定宏的定义时候,终于觉得我要用零宽断言来匹配位置了。

用的是python的 re模块,开始半天都匹配不上,因为是GUI程序,竟然也没有仔细看控制台的输出。后来仔细看了一下,又google了一下,看到两个匹配要取出字符串前面的零宽断言都必须是定长的时候……才明白怎么回事。

也搞不清楚什么负向之类的复杂中文名称,记录下我自己的理解:
(?=exp)真正有用的部分,在这个位置之前,这个位置需要匹配exp
(?<=exp)真正有用的部分,在这个位置之后,这个位置需要匹配exp
(?!exp)真正有用的部分,在这个位置之前,这个位置不匹配exp
(?<!exp)真正有用的部分,在这个位置之后,这个位置不匹配exp


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK