0

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?

2 Answers 2

1

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

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

Comments

1

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.

Comments

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.