and want to get the whole sequence for this ... e.g. for re....">

Python regex: get the whole group sequence

I have a regex like this '^(a|ab|1|2)+$' and want to get the whole sequence for this ...

e.g. for re.search (reg, 'ab1') I want to get ('ab', '1')

An equivalent result I can get with the pattern '^(a|ab|1|2)(a|ab|1|2)$' but I don't know how many blocks were matched (pattern) +

Is this possible, and if so, how?

+4
source share
3 answers

Your original expression matches the way you want, it just matches the entire line and does not display separate groups for each individual match. Using the repeat operator ('+', '*', '{m, n}'), the group is overwritten each time, and only the final match is saved. This is stated in the documentation :

If a group matches multiple times, only the last match is available.

+2
source

try the following:

 import re r = re.compile('(ab|a|1|2)') for i in r.findall('ab1'): print i 

The parameter ab was transferred to be the first, so it will match ab in favor of only a . The findall method matches your regular expression more and returns a list of matched groups. In this simple example, you only get a list of strings. Each row for one match. If you had more groups, you will get a list of tuples, each of which contains rows for each group.

This should work for your second example:

 pattern = '(7325189|7325|9087|087|18)' str = '7325189087' res = re.compile(pattern).findall(str) print(pattern, str, res, [i for i in res]) 

I am removing the ^$ characters from the pattern, because if findall has to find more than one substring, then it should search anywhere on str. Then I removed + so that it matches single occurrences of these parameters in the template.

+3
source

I think you don’t need regular expressions for this problem, you need a recursive graph search function

+2
source

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


All Articles