简体   繁体   English

正则表达式量词中的反向引用

[英]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 我需要找到两个值:abcdefghijklmnopqrst和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. 抢先评论:我知道一个人可以做"(.)\\1"来两次匹配相同的字符。 This is not what I mean with "self-referential", though. 但是,这并不是我所说的“自我参照”。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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