Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backreference in Regular Expression Quantifier

Tags:

c#

regex

I have a string that contains a header with a length of the following field.

Example:

fillerfillerCA20 abcdefghijklmnopqrst CA5 zyxwvfillerfiller

I need to find the two values: abcdefghijklmnopqrst and zyxwv

I was going to use a backreference to get the length for the quantifier:

(?i)ca(?<length>\d+?)\x20.{\k<length>}\x20?

but apparently, using a backreference in a quantifier is not supported.

How can I accomplish this?

like image 891
Matt Ruwe Avatar asked Sep 10 '25 17:09

Matt Ruwe


1 Answers

Not in one step. Regular expressions cannot be self-referential. They are first built, and then used. No re-building/augmenting is possible once the regex is built.

You can match the length info as you already do and use it in a second step, while evaluating the matches.

Preemptive comment: I know that one can do "(.)\1" to match the same character twice. This is not what I mean with "self-referential", though.

like image 133
Tomalak Avatar answered Sep 13 '25 06:09

Tomalak