Home > Blockchain >  Regular expression not returning everything on pattern in python
Regular expression not returning everything on pattern in python

Time:02-07

I'm using 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?

CodePudding user response:

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

CodePudding user response:

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.

  •  Tags:  
  • Related