tags:

views:

35

answers:

1

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 final 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
+1  A: 

In .NET, you would use

myMatch.Groups[1].Captures
Timothy Khouri
But if you're trying to parse mathematical expressions, forget about using regexes, even in .NET. That way lies madness. :-/
Alan Moore