0

I'm trying to parse a text document with data in the following format: 24036 -977. I need to separate the numbers into separate values, and the way I've done that is with the following steps.

values = re.search("(.*?)\s(.*)")
x = values.group(1)
y = values.gropu(2)

This does the job, however I was curious about why using (.*?) in the second group causes the regex to fail? I tested it in the online regex tester(https://regex101.com/r/bM2nK1/1), and adding the ? in causes the second group to return nothing. Now as far as I know .*? means to take any value unlimited times, as few times as possible, and the .* is just the greedy version of that. What I'm confused about is why the non greedy version.*? takes that definition to mean capturing nothing?

3

2 Answers 2

3

Because it means to match the previous token, the *, as few times as possible, which is 0 times. If you would it to extend to the end of the string, add a $, which matches the end of string. If you would like it to match at least one, use + instead of *.

The reason the first group .*? matches 24036 is because you have the \s token after it, so the fewest amount of characters the .*? could match and be followed by a \s is 24036.

Sign up to request clarification or add additional context in comments.

Comments

1

@iobender has pointed out the answer to your question.

But I think it's worth mentioning that if the numbers are separated by space, you can just use split:

>>> '24036 -977'.split()
['24036', '-977']

This is simpler, easier to understand and often faster than regex.

2 Comments

Yep, this is actually a better implementation. I've just mostly been doing text parsing in python, so I defaulted to regex, so it didn't occur to me to look for other things. Thanks
@JustinWang, no problem - I know many people who default to regex for the simplest things, always worth trying another route first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.