Python Forum
Regex to find triple characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regex to find triple characters
#11
(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.
Reply
#12
(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.
Reply
#13
(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?
« We can solve any problem by introducing an extra level of indirection »
Reply
#14
import re

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Find numbers using Regex giddyhead 18 3,404 Jul-28-2022, 12:29 AM
Last Post: giddyhead
  Find if chain of characters or number Frankduc 4 1,873 Feb-11-2022, 01:55 PM
Last Post: Frankduc
  Regex not finding all unicode characters tantony 3 2,347 Jul-13-2021, 09:11 PM
Last Post: tantony
  Find and replace in files with regex and Python Melcu54 0 1,900 Jun-03-2021, 09:33 AM
Last Post: Melcu54
  EOF while scanning triple-quoted string literal louis216 1 4,031 Jun-30-2020, 04:11 AM
Last Post: bowlofred
  How to find the first and last of one of several characters in a list of strings? tadsss 2 2,265 Jun-02-2020, 05:23 PM
Last Post: bowlofred
  Remove escape characters / Unicode characters from string DreamingInsanity 5 14,165 May-15-2020, 01:37 PM
Last Post: snippsat
  Find and replace to capitalize with Regex hermobot 2 2,584 Mar-21-2020, 12:30 PM
Last Post: hermobot
  Help converting MATLAB triple-for loop to Python davlovsky 1 2,044 Oct-29-2019, 10:26 PM
Last Post: scidam
  Do I always have to use triple quotes or \n for multi-line statements? DragonG 3 2,692 Oct-24-2018, 11:21 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020