Reputation: 117
I'm using https://regexr.com/ for testing the regex pattern with the following,
but when I try to use the same pattern in python I got
how can I rewrite the python pattern so I can retrieve the "state of " as in the regexr site?
Upvotes: 0
Views: 48
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
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