Grouping Regex question

Discussion in 'Java' started by gill.jp@gmail.com, Jan 29, 2007.

  1. Guest

    I am trying to get all the names i.e: SMITH J , BALTIMORE D, JONES AL

    String input = "NAME=(SMITH J) NAME=(BALTIMORE D) NAME=(JONES AL)
    JUNK=(ABC);
    Pattern pattern = Pattern.compile(" ");
    Matcher matcher = pattern.matcher(input);

    for (int i=0; i<=matcher.groupCount(); i++) {
    String groupStr = matcher.group(i);
    System.out.println("groupStr: "+ groupStr);
    }

    I played around with few patterns, I am unable to get all of the
    names.
    Anyone knows what this regex might be?

    Thanks, J
     
    , Jan 29, 2007
    #1
    1. Advertising

  2. Carl Guest

    On Jan 29, 1:42 pm, wrote:
    > I am trying to get all the names i.e: SMITH J , BALTIMORE D, JONES AL
    >
    > String input = "NAME=(SMITH J) NAME=(BALTIMORE D) NAME=(JONES AL)
    > JUNK=(ABC);
    > Pattern pattern = Pattern.compile(" ");
    > Matcher matcher = pattern.matcher(input);
    >
    > for (int i=0; i<=matcher.groupCount(); i++) {
    > String groupStr = matcher.group(i);
    > System.out.println("groupStr: "+ groupStr);
    >
    > }I played around with few patterns, I am unable to get all of the
    > names.
    > Anyone knows what this regex might be?
    >
    > Thanks, J


    Something like this should get you started:

    Matcher matcher = Pattern.compile("NAME=\\((.*?)\\)").matcher(input);
    while(matcher.find()) {
    System.out.println(matcher.group(1));
    }

    Hope that helps,
    Carl.
     
    Carl, Jan 29, 2007
    #2
    1. Advertising

  3. Guest

    Thanks Carl. Got it to work.
     
    , Jan 30, 2007
    #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. Brian Henry

    radio buttons on form grouping question

    Brian Henry, Nov 20, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    5,090
    Jon Paugh
    Nov 20, 2003
  2. Replies:
    3
    Views:
    775
    Reedick, Andrew
    Jul 1, 2008
  3. John Galenski

    XSLT Grouping and Counting Question

    John Galenski, Feb 6, 2009, in forum: XML
    Replies:
    1
    Views:
    1,932
    Martin Honnen
    Feb 6, 2009
  4. galyle
    Replies:
    4
    Views:
    165
    galyle
    Oct 11, 2011
  5. Replies:
    6
    Views:
    136
    Florian GroƟ
    Jun 27, 2005
Loading...

Share This Page