regex: How to extract substrings?

Discussion in 'Java' started by Markus Dehmann, Dec 10, 2005.

  1. This should be really easy, but I couldn't find it in the tutorials and
    documentations on the web:

    What is the Java equivalent to the following Perl operation?

    my $entry = "__3432__Smith__"
    my ($id,$name) =
    $entry =~ m/__(\d+)__([A-z]+)__/;

    In other words, I want to extract the number and the name from the
    string $entry, using a regular expression.

    I tried the following:

    Pattern p = Pattern.compile("__([0-9]+)__([A-z]+)__");
    Matcher m = p.matcher("__3432__Smith__");
    while(m.find()){
    System.out.println(m.group());
    }


    But that gives me the complete match at once, not the two subgroups that
    I specified using the parens: (\d+) and ([A-z]+).

    Who can help?
    Thanks!
    Markus Dehmann, Dec 10, 2005
    #1
    1. Advertising

  2. Markus Dehmann wrote:
    > This should be really easy, but I couldn't find it in the tutorials and
    > documentations on the web:
    >
    > What is the Java equivalent to the following Perl operation?
    >
    > my $entry = "__3432__Smith__"
    > my ($id,$name) =
    > $entry =~ m/__(\d+)__([A-z]+)__/;
    >
    > In other words, I want to extract the number and the name from the
    > string $entry, using a regular expression.
    >
    > I tried the following:
    >
    > Pattern p = Pattern.compile("__([0-9]+)__([A-z]+)__");
    > Matcher m = p.matcher("__3432__Smith__");
    > while(m.find()){
    > System.out.println(m.group());
    > }
    >
    >
    > But that gives me the complete match at once, not the two subgroups that
    > I specified using the parens: (\d+) and ([A-z]+).
    >
    > Who can help?
    > Thanks!


    From the docs on Matcher

    "Capturing groups are indexed from left to right, starting at one. Group
    zero denotes the entire pattern, so the expression m.group(0) is
    equivalent to m.group()."

    So to answer your question:

    Pattern p = Pattern.compile("__(\\d+)__(\\w+)__");
    Matcher m = p.matcher($entry);
    if (m.matches()) {
    System.out.println(m.group(1));
    System.out.println(m.group(2));
    }

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Dec 10, 2005
    #2
    1. Advertising

  3. Markus Dehmann

    IchBin Guest

    Markus Dehmann wrote:
    > This should be really easy, but I couldn't find it in the tutorials and
    > documentations on the web:
    >
    > What is the Java equivalent to the following Perl operation?
    >
    > my $entry = "__3432__Smith__"
    > my ($id,$name) =
    > $entry =~ m/__(\d+)__([A-z]+)__/;
    >
    > In other words, I want to extract the number and the name from the
    > string $entry, using a regular expression.
    >
    > I tried the following:
    >
    > Pattern p = Pattern.compile("__([0-9]+)__([A-z]+)__");
    > Matcher m = p.matcher("__3432__Smith__");
    > while(m.find()){
    > System.out.println(m.group());
    > }
    >
    >
    > But that gives me the complete match at once, not the two subgroups that
    > I specified using the parens: (\d+) and ([A-z]+).
    >
    > Who can help?
    > Thanks!



    Actually, Roedy has a nice reference for this

    http://mindprod.com/jgloss/regex.html
    --


    Thanks in Advance...
    IchBin, Pocono Lake, Pa, USA
    http://weconsultants.servebeer.com/JHackerAppManager
    __________________________________________________________________________

    'If there is one, Knowledge is the "Fountain of Youth"'
    -William E. Taylor, Regular Guy (1952-)
    IchBin, Dec 10, 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. Timothy Hume
    Replies:
    2
    Views:
    483
    Brian Szmyd
    Sep 10, 2004
  2. Amit Khemka
    Replies:
    8
    Views:
    298
    Amit Khemka
    Nov 23, 2005
  3. Replies:
    3
    Views:
    730
    Reedick, Andrew
    Jul 1, 2008
  4. RolfK
    Replies:
    1
    Views:
    1,835
    Martin Honnen
    Jun 7, 2009
  5. Replies:
    12
    Views:
    185
    Tad McClellan
    May 16, 2006
Loading...

Share This Page