Regex not returning full match, just the start
Question:
I’d like this regex to return the full text-plus-number e.g. Indy-500 or Independent-500, but it seems to chop it off.
> re.findall('(Independent|Indy)-d+', "In the Indy-500 or Independent-1000 there was a company")
['Indy', 'Independent']
How can I fix this? It works perfectly if I only search for Indy-d+
e.g.
> re.findall('Indy-d+', "In the Indy-500 or Independent-1000 there was a company")
['Indy-500']
but I would like it to match a range of prefixes.
Answers:
Use non-capturing group to clarify the alternatives:
>>> re.findall(r'(?:Independent|Indy)-d+', "In the Indy-500 or Independent-1000 there was a company")
['Indy-500', 'Independent-1000']
Then the entire match is shown.
Or use a capturing group around the entire thing you want with a non-capturing group around the alternative prefixes like so:
r'((?:Independent|Indy)-d+)'
so that the alternative does not become
(?:Independent)|(?:Indy-d+)
which is what Independent|Indy-d+
would be…
I’d like this regex to return the full text-plus-number e.g. Indy-500 or Independent-500, but it seems to chop it off.
> re.findall('(Independent|Indy)-d+', "In the Indy-500 or Independent-1000 there was a company")
['Indy', 'Independent']
How can I fix this? It works perfectly if I only search for Indy-d+
e.g.
> re.findall('Indy-d+', "In the Indy-500 or Independent-1000 there was a company")
['Indy-500']
but I would like it to match a range of prefixes.
Use non-capturing group to clarify the alternatives:
>>> re.findall(r'(?:Independent|Indy)-d+', "In the Indy-500 or Independent-1000 there was a company")
['Indy-500', 'Independent-1000']
Then the entire match is shown.
Or use a capturing group around the entire thing you want with a non-capturing group around the alternative prefixes like so:
r'((?:Independent|Indy)-d+)'
so that the alternative does not become
(?:Independent)|(?:Indy-d+)
which is what Independent|Indy-d+
would be…