tags:

views:

550

answers:

2

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?

+3  A: 

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.

Tomalak
A: 

Better build a parser: Look up the positions of CA, read the following digit characters and then read the next x characters as described by the digit value.

Gumbo