regex group(1) problem

Discussion in 'Java' started by bahhab@hotmail.com, May 28, 2005.

  1. Guest

    Hi

    The first code snip works but takes 4 lines and as there will be a
    number of pattern matches I'd rather do each on one line. The second
    snip is how I think it should work except I get "cannot resolve symbol
    symbol : variable matcher". Is this possible and if so what should my
    variable "matcher" be?

    <snip>
    Pattern regex = Pattern.compile("^(LRC[0-9]{5}).*");
    Matcher match = regex.matcher(line);
    boolean b = match.matches();
    if (b) {id = match.group(1);}
    <snip>

    <snip2>
    if (Pattern.compile("^(LRC[0-9]{5}).*").matcher(line).matches()) { /
    id = matcher.group(1);}
    <snip2>
     
    , May 28, 2005
    #1
    1. Advertising

  2. hiwa Guest

    wrote:

    >Hi
    >
    >The first code snip works but takes 4 lines and as there will be a
    >number of pattern matches I'd rather do each on one line. The second
    >snip is how I think it should work except I get "cannot resolve symbol
    >symbol : variable matcher". Is this possible and if so what should my
    >variable "matcher" be?
    >
    ><snip>
    >Pattern regex = Pattern.compile("^(LRC[0-9]{5}).*");
    >Matcher match = regex.matcher(line);
    >boolean b = match.matches();
    >if (b) {id = match.group(1);}
    ><snip>
    >
    ><snip2>
    >if (Pattern.compile("^(LRC[0-9]{5}).*").matcher(line).matches()) { /
    >id = matcher.group(1);}
    ><snip2>
    >
    >
    >

    In your <snip2> code, Matcher object is unnamed.
     
    hiwa, May 29, 2005
    #2
    1. Advertising

  3. Filip Larsen Guest

    wrote

    > Pattern regex = Pattern.compile("^(LRC[0-9]{5}).*");
    > Matcher match = regex.matcher(line);
    > boolean b = match.matches();
    > if (b) {id = match.group(1);}


    If you want to compact the code you can replace the variables that are
    only referenced once (regex and b) with their initializer value:

    Matcher matcher = Pattern.compile("^(LRC[0-9]{5}).*").matcher(line);
    if (matcher.matches()) id = matcher.group(1);


    Regards,
    --
    Filip Larsen
     
    Filip Larsen, May 29, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mikea59

    regex-group() problem

    mikea59, Jan 6, 2005, in forum: XML
    Replies:
    2
    Views:
    734
    mikea59
    Jan 6, 2005
  2. Akaketwa
    Replies:
    1
    Views:
    4,967
    impaler
    Sep 22, 2006
  3. Replies:
    3
    Views:
    797
    Reedick, Andrew
    Jul 1, 2008
  4. christopher taylor
    Replies:
    0
    Views:
    421
    christopher taylor
    Sep 17, 2008
  5. RolfK
    Replies:
    1
    Views:
    1,893
    Martin Honnen
    Jun 7, 2009
Loading...

Share This Page