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?
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?
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')]
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.