waaat
waaat

Reputation: 117

Regular expression not returning everything on pattern in python

I'm using https://regexr.com/ for testing the regex pattern with the following,

enter image description here

but when I try to use the same pattern in python I got

enter image description here

how can I rewrite the python pattern so I can retrieve the "state of " as in the regexr site?

Upvotes: 0

Views: 48

Answers (2)

Simon Fromme
Simon Fromme

Reputation: 3174

Assuming you want to match patterns such as "state of ny" or "state that is named new york", you should put the entire string you want to capture into parentheses and use a non-capturing group for the alternative.

re.findall(r'(state.*.(?:ny|new york))', " consuption state of ny  ")

Additionally, .*. could be written as .+ to be a little more concise.

Upvotes: 1

Christoph Burschka
Christoph Burschka

Reputation: 4689

Put the whole string you want to capture into parentheses. Note that with multiple capture groups, the result will be a list of tuples (the first element of the tuple will be the first capture group):

import re
re.findall(r'state.*.(ny|new york)', 'consumption state of ny ')

['ny']

re.findall(r'(state.*.(ny|new york))', 'consumption state of ny ')

[('state of ny', 'ny')]

Upvotes: 1

Related Questions