开发者

Regex: Is it possible to reference all of the repeats of a capture?

Consider this regex: ^([0-9]+)(?: ([-\+/%\*]) ([0-9]+))+$

In english, it says "a number, followed by one or more of (an operation and another number)".

Matched against a subject like 5 + 4 that regex produces $1 = 5, $2 = +, and $3 = 4.

Matched against this subject: 5 + 4 * 3, I get $1 = 5, $2 = *, and $3 = 3.

In this latter case, can I capture and reference the + and the 4 ?

In other words, when using a quantifier in the regex, can I reference sequences other than the fin开发者_C百科al captured sequence?

If the answer depends n the language or platform, I guess I am most interested in:

  • C, with PCRE
  • Javascript
  • .NET, with System.Text.RegularExpressions


In .NET, you would use

myMatch.Groups[1].Captures
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜