Check if String.matches() AND (if yes) extract number from String?

Discussion in 'Java' started by Jochen Brenzlinger, Nov 21, 2011.

  1. Assume I have a String var and value like:

    String var = new String("foobar[345]");

    Now I want to check if this string matches a certain pattern and if yes extract the number into a long var.
    The first part is easy:

    if var.matches("\\w*\[\\d+\]") {
    long l = ????; }

    ....but I have no idea on how to extract the number.
    How can this be achieved?

    Jochen
     
    Jochen Brenzlinger, Nov 21, 2011
    #1
    1. Advertising

  2. Basically, you use the java.util.regex classes.

    here's a unit test that illustrates the technique:

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    @Test
    public void extractIndex() throws Exception
    {
    String source = "Foo[345]";
    Pattern pattern = Pattern.compile("\\w+\\[(\\d+)\\]");
    Matcher matcher = pattern.matcher(source);
    matcher.find();
    assertThat(matcher.groupCount(), is(1));
    String index = matcher.group(1);
    assertThat(index, is("345"));
    }
     
    Henk van Voorthuijsen, Nov 21, 2011
    #2
    1. Advertising

  3. Jochen Brenzlinger

    Tassilo Horn Guest

    (Jochen Brenzlinger) writes:

    > Assume I have a String var and value like:
    >
    > String var = new String("foobar[345]");
    >
    > Now I want to check if this string matches a certain pattern and if
    > yes extract the number into a long var.


    You are looking for Capturing Groups. Have a look at
    java.util.regex.Pattern and Matcher. You need something along these
    lines (untested):

    String foo = "bla[123]";
    Pattern myPattern = Pattern.compile("\\w+\\[(\\d+)\\]");
    Matcher m = myPattern.matcher(foo);
    if (m.find()) {
    long idx = Long.parseLong(m.group(1));
    // idx should be 123 here
    }

    Bye,
    Tassilo
    --
    (What the world needs (I think) is not
    (a Lisp (with fewer parentheses))
    but (an English (with more.)))
    Brian Hayes, http://tinyurl.com/3y9l2kf
     
    Tassilo Horn, Nov 21, 2011
    #3
  4. Jochen Brenzlinger

    Roedy Green Guest

    On 21 Nov 2011 15:04:48 GMT, (Jochen Brenzlinger)
    wrote, quoted or indirectly quoted someone who said :

    >if var.matches("\\w*\[\\d+\]") {
    > long l = ????; }
    >
    >...but I have no idea on how to extract the number.
    >How can this be achieved?


    see http://mindprod.com/jgloss/regex.html
    for examples.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    I can't come to bed just yet. Somebody is wrong on the Internet.
     
    Roedy Green, Nov 21, 2011
    #4
  5. Jochen Brenzlinger

    Stefan Ram Guest

    (Jochen Brenzlinger) writes:
    >if var.matches("\\w*\[\\d+\]") {
    > long l = ????; }
    >...but I have no idea on how to extract the number.


    When you already know that »var« does match, you can use:

    java.lang.Long.valueOf( var.replaceAll( "\\D+(\\d+).", "$1" ))
     
    Stefan Ram, Nov 21, 2011
    #5
  6. Jochen Brenzlinger

    Daniel Pitts Guest

    On 11/21/11 11:36 AM, Stefan Ram wrote:
    > (Jochen Brenzlinger) writes:
    >> if var.matches("\\w*\[\\d+\]") {
    >> long l = ????; }
    >> ...but I have no idea on how to extract the number.

    >
    > When you already know that »var« does match, you can use:
    >
    > java.lang.Long.valueOf( var.replaceAll( "\\D+(\\d+).", "$1" ))
    >
    > .
    >

    Technically correct specific to this example. However, if you have a
    more complicated pattern it won't necessarily work.

    For example, \w*\[\d+\](?:\w+(\d+))?\w*(\d+)
     
    Daniel Pitts, Nov 21, 2011
    #6
    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. teo
    Replies:
    3
    Views:
    570
  2. Jeremy
    Replies:
    10
    Views:
    1,346
    Tim Chase
    Jan 13, 2010
  3. Guest

    Check Yes/No cookies enabled?

    Guest, Aug 5, 2004, in forum: ASP General
    Replies:
    6
    Views:
    163
    Guest
    Aug 5, 2004
  4. Jamie Herre
    Replies:
    1
    Views:
    228
    why the lucky stiff
    Jan 7, 2005
  5. Ingo Weiss

    Get Number of regex matches

    Ingo Weiss, Dec 6, 2006, in forum: Ruby
    Replies:
    5
    Views:
    208
    Ingo Weiss
    Dec 7, 2006
Loading...

Share This Page