简体   繁体   中英

regex : match string with invert pattern preceeding the other pattern

this is the code :

(?!cxdef\\.)trimheader

i want it to match any : '=trimheaderspace' or 'old.trimheader' or 'get trimheader()' etc... but NOT "... = cxdef.trimheaderspace()"

i don't want a 'cxdef.' preceeding the 'trimheader'.

It looks so simple , but the code won't work . any idea ?

You need to use a zero width negative lookbehind:

>>> def out_match(st):
...     return st if re.search(r'(?<!cxdef\.)trimheader', st) else None
... 
>>> out_match('=trimheaderspace')
'=trimheaderspace'
>>> out_match('cxdef.trimheaderspace()')
>>> 

I found the answer for my question , here the code :

(?<!cxdef\.)trimheader

i just add < inbetween the ? and ! to make negative lookbehind. it will match anything other than 'cxdef.trimheader'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM