-1

I want to find all "n't" in a sentence with this Regex 'n't [0-9]+(\.[0-9][0-9]?)?'. And its working fine in RegExr:

enter image description here

but When I try to do it with this code, it does not work:

txt = "japan isn't 56 country in Europe."
nt = re.findall(r"n't [0-9]+(\.[0-9][0-9]?)?",txt)
print(nt)

2 Answers2

0

findall is slightly weird when it comes to parentheses. Once you have them in there, it only returns the result of that group, not of the entire match. You can make the parentheses non-capturing:

>>> nt = re.findall(r"n't [0-9]+(?:\.[0-9][0-9]?)?",txt)
>>> print(nt)
["n't 56"]
mrks
  • 8,033
  • 1
  • 33
  • 62
0

This is a subtle problem with your script, which the following fixes:

txt = "japan isn't 56 country in Europe."
nt = re.findall(r"n't [0-9]+(?:\.[0-9][0-9]?)?",txt)
print(nt)    # prints ["n't 56"]

In your original call to re.findall, you were using this pattern:

n't [0-9]+(\.[0-9][0-9]?)?

This means that the first capture group is the optional term .123. With the re.findall API, if you specify a capture group, then it is what will be returned. Given that your input did not contain this group, your resulting list was empty. In my corrected version, I made the capturing group inactive, using ?:. If you don't specify any explicit capture groups, then the entire matching pattern will be returned, which is the behavior you want here.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 1
    *I made the group optional, using `?:`* - no, you did not. With `(?:...)`, you made the capturing group [non-capturing](https://stackoverflow.com/questions/3512471/what-is-a-non-capturing-group-in-regular-expressions). Optional groups are made as such with `?` quantifier after them, ``(?:...)?`` or `(...)?`. – Wiktor Stribiżew Nov 03 '20 at 08:13