简体   繁体   中英

How to match a string and ignore everything after it?

So here is the deal, I want to match a number and a string that comes after it. Like a buying list, "10 bananas" or "15 apples". The problem is, the same regex has to match "10 banana" but can't match "10 banana milkshake".

Regexes I already tried:

r"\b[0-9]{1,5}.banana"
r"\b[0-9]{1,5}.banana\b"
r"\b[0-9]{1,5}.banana$"

Example:

8 banana 3 banana ice cream 2 banana milkshake 5 banana

From that string, I need to match only "8 banana" and "5 banana".

With the regexes I tried, it matches "X banana" as expected, but I want it to be exactly "X banana" (where X is a number that appears 1-5 times, as in the regex) and ignore if it has anything else after it, like "ice cream" or "milkshake" in this example. Also, the "$" sign doesn't match anything if I put it on the regex.

use re.findall with lookahead regex below, it assert banana must be following by digit or end of string. s? after banana to match plural bananas as well

import re
s = '8 banana 3 banana ice cream 2 banana milkshake 5 banana'
match = re.findall(r'\b[0-9]{1,5}.bananas?(?=\s?\d+|$)', s)
# ['8 banana', '5 banana']

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