简体   繁体   中英

Backreference in Regular Expression Quantifier

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?

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.

更好地构建解析器:查找CA的位置,读取以下数字字符,然后读取数字值所描述的下一个x字符。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM