Bracket matching in python regular expressions

  • 2020-04-02 14:26:33
  • OfStack

Question:

M = re the.findall (' [0-9] [0-9] * 4 * ', '[4])
It can match up to 4.
M = re the.findall (' ([0-9]) * 4 ([0-9]) * ', '[4])
It doesn't match 4.
Why is that? PS, this is a simplified description, and I'm going to use regularization which is more complicated than this, so I'm going to use (), which is a sequence match.
As a side note, when I put it in notepad++, it works both ways, so I don't know why it doesn't work in python.

The answer:

Python's regular USES () match, so the result is [',''], which is a match between two (). To achieve the original matching effect, is to match out the 4, there are two solutions:

1. Add a curly braces, the outermost layers became: m = re the.findall (' (([0-9]) * 4 ([0-9]) *) ', '[4]), return the result of the first element is the matching results.
2. Return the match result of removing (), and add ? before the parenthesis; :, becomes m = re. Findall ('(? : \ d) * 4 (the & # 63; :\d)*', '[4]'), return the matching result.


Related articles: