0

in python what would be matched by "(4{5,6})\1"?

explain

14th Oct 2018, 4:03 AM
Aboo Backer
Aboo Backer - avatar
5 Réponses
+ 4
Looks like a regular expression. (4{5,6}) is a 4 that is repeated five or six times. The whole expression is a capturing group because of the parentheses \1 is one repetition of the first capturing group. So the expression would match 4444444444 or 444444444444 (=10 or 12 times '4')
14th Oct 2018, 5:28 AM
Anna
Anna - avatar
+ 4
Actually, I'd expect it to find matches in *all* strings which have at least 10 consecutive 4s. In order for the capturing group to be repeated, there should be at least ten 4s present, but not necessarily *exactly* tens or twelves of those. For example, if you have a string containing 16 or 23 4s -- it will also match.
14th Oct 2018, 6:49 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 4
I'd say that depends on the regex method that is used. import re ptrn = re.compile(r'(4{5,6})\1') s = '807654354444444444123456djs' print(ptrn.search(s)) # <_sre.SRE_Match object; span=(8, 18), match='4444444444'> print(ptrn.match(s)) # None s = '444444444444444444444444444444444' print(ptrn.search(s)) # <_sre.SRE_Match object; span=(0, 12), match='444444444444'> print(ptrn.match(s)) # <_sre.SRE_Match object; span=(0, 12), match='444444444444'> for t in ptrn.finditer(s): print('>>>', t) # >>> <_sre.SRE_Match object; span=(0, 12), match='444444444444'> # >>> <_sre.SRE_Match object; span=(12, 24), match='444444444444'>
14th Oct 2018, 7:01 AM
Anna
Anna - avatar
+ 4
Anna A-greedy ;)
14th Oct 2018, 7:33 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 2
10 or 12 fours
10th Dec 2020, 9:10 AM
Diana Chepkirui