简体   繁体   中英

Regex, return first match after specific word / Python

EG We have this sentence.

Sample 987 abc sample 567 xyz, yellow world sample 123

By using this regex = sample \d+

I would like, by using re.findall() to get values next to the sample which is after word abc , which is sample 567 and sample 123

I know how to find the value I need, the problem is that I need to use it AFTER a specific word and not sure how to.

PS This word can be changed from abc to word so the result will be sample 123 and etc....

The easiest way might be to limit the regex search to a specific area:

pattern = re.compile(r'sample \d+')
start_pos = original_string.index('your_start_word')
matches = pattern.findall(original_string, start_pos)

Right, it looks like the following may work for you:

\bsample (\d+)(?!.*\babc\b)

This will assure that the word "abc" is not following, therefor it does not capture '987' from your sample.

See the online demo

  • \b - A word-boundary.
  • sample - Match "sample " literally.
  • (\d+) - Capture 1+ digits in a capture group.
  • (?..*\babc\b) - Negative lookahead to prevent it be in front of the word "abc".

For example:

import re
s = 'sample 987 abc sample 567 xyz, yellow world sample 123'
results = re.findall(r'(?<=\bsample )\d+(?!.*\babc\b)', s)
print(results)

Prints:

['567', '123']

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