Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expressions Dependant on Previous Matchings

Tags:

python

regex

For example, how could we recognize a string of the following format with a single RE:

LenOfStr:Str

An example string in this format is:

5:5:str

The string we're looking for is "5:str".

In python, maybe something like the following (this isn't working):

r'(?P<len>\d+):(?P<str>.{int((?P=len))})'

In general, is there a way to change the previously matched groups before using them or I just asked yet another question not meant for RE.

Thanks.

like image 958
user1642394 Avatar asked Dec 17 '25 03:12

user1642394


1 Answers

Yep, what you're describing is outside the bounds of regular expressions. Regular expressions only deal with actual character data. This provides some limited ability to make matches dependent on context (e.g., (.)\1 to match the same character twice), but you can't apply arbitrary functions to pieces of an in-progress match and use the results later in the same match.

You could do something like search for text matching the regex (\d+):\w+, and then postprocess the results to check if the string length is equal to the int value of the first part of the match. But you can't do that as part of the matching process itself.

like image 177
BrenBarn Avatar answered Dec 19 '25 16:12

BrenBarn