crackjack
crackjack

Reputation: 11

Regular expression to stop at first match immediately

So this is a given string where you can see in some places there are missing values. We have to fill those missing values in a certain specified way.

s = "_, _, 30, _, _, _, 50, _, _ "

My concern for the first bit of the problem is to extract the " _, _, 30 " part from the string ( so that i can take it apart, modify and replace the modifed bit in the original string ). I tried to do it using:

import re
res = re.findall("_.*[0-9]",s)
print(res)

The output I am getting is:

_, _, 30, _, _, _, 50

whereas the desired result is:

_, _, 30

How can i do it using re module?

Upvotes: 1

Views: 612

Answers (1)

Georgina Skibinski
Georgina Skibinski

Reputation: 13387

Your problem is coming from the fact, that on default regex operators are greedy - which means they return the longest match, there are 2 ways to solve your problem:

(1) Just to move from greedy to non-greedy operator:

>>> re.findall("_.*?[0-9]+",s)
['_, _, 30', '_, _, _, 50']

(2) Replace "any" with non-numeric:

>>> re.findall(r"[^0-9]*[0-9]+", s)
['_, _, 30', ', _, _, _, 50']

Upvotes: 1

Related Questions