Regex: possibly two patterns found in one text

Toyo

I have a specific pattern but the text to be process can change randomly.
The text I am trying to filter currently using regex (Python.re.findall, python v3.9.13) is as follow:
"ABC9,10.11A5:6,7:8.10BC1"

I am using the following regex expression: r"([ABC]{1,})(([0-9]{1,}[,.:]{0,}){1,})"

The current result is:
[("ABC", "9,10.11", "11"), ("A", "5:6,7:8.10", "10"), ("BC", "1", "1")]

What I am looking for as result should be:
[("ABC", "9,10.11"), ("A", "5:6,7:8.10"), ("BC", "1")]

I don't understand why the last number in the second part is always repeated again.
Please help.

Nick

I presume you are using re.findall, since that returns the contents of all capture groups in its output. In your case the last number repetition is due to the capture group around [0-9]{1,}[,.:]{0,}. Making that a non-capturing group resolves the issue:

([ABC]{1,})((?:[0-9]{1,}[,.:]{0,}){1,})

In python:

re.findall(r"([ABC]{1,})((?:[0-9]{1,}[,.:]{0,}){1,})", s)
# [('ABC', '9,10.11'), ('A', '5:6,7:8.10'), ('BC', '1')]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

RegEx: Match one of two patterns

Return True if two patterns are found in text with powershell

Regex to extract text between two character patterns

I got two patterns but I need one regex solution

Python regex - Extract all the matching text between two patterns

Regular expression to capture n lines of text between two regex patterns

regex of two patterns

Regex to extract one or two values from text

How search for specific text in two columns with multiple criteria and with possibly more than one instance of key field?

Merging two directed (possibly cyclic) graphs into one

Consolidating 2 regex patterns into one

Multiple Patterns and substitutions with one regex

Return match if one match from each of two patterns found using python spaCy PhraseMatcher

regex have possibly one underscore and then at least one legal character

Regex match only if multiple patterns found (python)

Ignore later patterns in python regex if previous patterns are not found

Extract two patterns at once using regex

regex with or condition to verify two different patterns?

Regex for single delimiter between two patterns

how to merge two models of regex patterns?

Regex: extract characters from two patterns

RegEx Match everything between two patterns (javascript)

Regex to find two patterns and extract values

How do I combine these two regex patterns?

Delete string between two regex patterns

Replace two patterns at the same regex in JavaScript

What is the different between these two RegEx patterns

Replace multiple regex patterns one pass

Regex for returning several patterns within one string

TOP Ranking

HotTag

Archive