February 11, 2002 Re: regexp suggestion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | "Pavel Minayev" <evilone@omen.ru> wrote in message news:a485eh$2rbg$1@digitaldaemon.com... > char[][] is the list of tokens, or, to be more exact, the list of their _values_. But how do I know their _types_ (string or number or ..)? Suppose > the regexp was: > > ([A-Za-z_]+|0-9+) > > And I get 10 tokens. How do I tell if the first matched [A-Za-z_]+ part or the 0-9+ part, without checking it separately (which results in two checks per token)? You can tell which parenthesized subexpression matched by checking to see which index it was in: char[][] m; r = new RegExp("(a)|(b)", "g"); // search for "a" or "b" while ((m = r.exec("a b and a b")) != null) { if (m[1]) ; // matched an "a" else if (m[2]) ; // matched a "b" } |
February 11, 2002 Re: regexp suggestion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | On Mon, 11 Feb 2002 14:57:58 -0800, "Walter" <walter@digitalmars.com> wrote:
>
> "Pavel Minayev" <evilone@omen.ru> wrote in message news:a485eh$2rbg$1@digitaldaemon.com...
> > char[][] is the list of tokens, or, to be more exact, the list of their _values_. But how do I know their _types_ (string or number or ..)?
> Suppose
> > the regexp was:
> >
> > ([A-Za-z_]+|0-9+)
> >
> > And I get 10 tokens. How do I tell if the first matched [A-Za-z_]+ part or the 0-9+ part, without checking it separately (which results in two checks per token)?
>
> You can tell which parenthesized subexpression matched by checking to see which index it was in:
>
> char[][] m;
> r = new RegExp("(a)|(b)", "g"); // search for "a" or "b"
> while ((m = r.exec("a b and a b")) != null)
> {
> if (m[1])
> ; // matched an "a"
> else if (m[2])
> ; // matched a "b"
> }
>
Or:
m = r.exec (...);
switch (m.length) {
case 0: // no match
case 2: // matched 'a'
case 3: //matched 'b'
...
???
|
Copyright © 1999-2021 by the D Language Foundation