简体   繁体   中英

What do the “(?<!…)” symbols mean in a Python regular expression?

I have the regular expression re.sub(r"(?<!\\s)\\}", r' }', string) . What does the (?<!…) sequence indicate?

It's a bit more than the < symbol, in the regular expression you've provided.

What's actually there is a 'Negative lookbehind': (?<! ) which is saying "What's before this is not...". In your case, it's looking for } , on the condition that what comes before it is not \\s - whitespace (tabs, spaces...)

Its a lookback. See the explanation here: http://www.rexegg.com/regex-disambiguation.html#negative-lookbehind

Quoted from the source:

Negative Lookbehind After the Match: \\d{3}(?<!USD\\d{3})
Explanation: \\d{3} matches 100 , then the negative lookbehind (?<!USD\\d{3}) asserts that at that position in the string, what immediately precedes is not the characters " USD " then three digits.

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