Python regex

What is the difference between '{m}' and '{m, n}?' in http://docs.python.org/library/re.html said '{m, n}?' matches numbers ranging from m to n times, but this is not a greedy search. Therefore, if its not a greedy search will match only m, no matter what?

+3
source share
2 answers

{m,n}?will preferably correspond only to mrepetitions, but it will expand as necessary to nrepetitions, if necessary for a longer match.

Compare ^x{2}y$and ^x{2,4}?y$:

The former will fail on xxxy, while the latter will match.

Summarizing:

x{m}: x m .

x{m,n}: x n , , , m ( ).

x{m,n}?: x m , , , n times ( ).

+14

:

>>> re.match(r'(x{1,3}?)(.*)', 'xxxxx').groups()
('x', 'xxxx')
>>> re.match(r'(x{1,3})(.*)', 'xxxxx').groups()
('xxx', 'xx')

, {n, m} {n, m}? ; , , , , .

+1

Source: https://habr.com/ru/post/1789675/


All Articles