February 11, 2002
"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
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'
    ...
???






1 2 3 4
Next ›   Last »