简体   繁体   中英

How to match all words that does not contain two digits in a row?

I have list of values, every one contains "pt" + 2 digits part. But if after that there is "_digitdigit" - this value should be exculded.

should match: data_pt01_pr, data_pt02_1_pr, data_pt02_2

should not match: data_pt01_01_pr, data_pt22_22, data_pt01_00_ABCD_1_bk_vi

I was thinking something like [^\\d{2}] will do the trick, but it doesn't work as I expected.

Use a negative lookahead assertion (?!...) :

pattern = r'pt\d\d(?!_\d\d)'

This pattern matches pt followed by two digits only if they are not followed by an underscore and two digits.

>>> p = re.compile(pattern)
>>> bool(p.search("data_pt01_pr"))
True
>>> bool(p.search("data_pt02_2"))
True
>>> bool(p.search("data_pt01_01_pr"))
False
>>> bool(p.search("data_pt22_22"))
False

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