+ 6
Python reg-ex multiple consecutive occurences of patterns problem
I have this code https://code.sololearn.com/c6afHry3Wrop/?ref=app in which I have put some questions in comments. Could anybody give answers to the questions, please?
2 Respostas
+ 7
The problem here is that Python's re.findall returns all matched groups. What happens when you do ([A-Z][0-9]){2} is that it matches one group and returns 'P5', instead of returning the full match (** Note: {2} was placed outside the parenthesis, so technically it wasn't part of the group). Using non-capturing groups, denoted by a prefix of ?: , as such:
(?:[A-Z][0-9]){2}
solves the first issue, second issue, and also the third (I guess).
** Just FYI, I originally tried to do something like
(([A-Z][0-9]){2})
... which doesn't exactly solve the problem, but you may observe some interesting re.findall behavior, which reaffirms the root cause of the issue.
P.S. If you are having regex issues with Python, I find this site handy:
https://pythex.org
+ 4
Thank you Hatsy Rei for your explanation 😊 and the hint to pythex.org. This website I did not know yet.