Python Forum

Full Version: Regex to find triple characters
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
(May-14-2024, 12:51 PM)Gribouillis Wrote: [ -> ]
(May-14-2024, 12:29 PM)bfallert Wrote: [ -> ]there is a compilation error stating that groups are not supported in lookbehinds.
Which version of Python are you using? It works fine here in Python 3.10. The latest Python is 3.12 as of may 2024. Groups are allowed in lookbehind assertions since Python 3.5 (2015).
I am using version 3.12 with the PyCharm IDE. It underlines the \1\1 in the first part of the pattern after the ! and says 'Group reference not allowed inside lookbehind'. It still runs but remains flagged as an error. I am on a mac but do not know if that is part of the issue.
(May-14-2024, 01:08 PM)deanhystad Wrote: [ -> ]
(May-14-2024, 12:27 PM)bfallert Wrote: [ -> ]Thank you, but this expression still allows matching consecutive characters other than exactly 3. Doubles, quads or others beside triples should not be found.

Change the repeat count from {1,} to {2}.

Still did not work quite right. If the string only contains 'GGG' it should show a result. If the string contains 'GGGG' it should not show a result since the matching characters are more than 3.
(May-14-2024, 01:22 PM)bfallert Wrote: [ -> ]It still runs but remains flagged as an error. I am on a mac but do not know if that is part of the issue.
If it runs it means that there is no error. I don't know PyCharm so this may be the problem. What happens if you run it out of PyCharm?
import re

matches = [match.group() for match in re.finditer(r"(.)\1{2,}", "AAAbbcDDDEDGGGG") if len(match.group()) == 3]
print(matches)
it wont work with white characters
([a-z]|[A-Z]|[1-9]){3}
Pages: 1 2