Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #10148 > unrolled thread

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

Started byjochen2@brenz.com (Jochen Brenzlinger)
First post2011-11-21 15:04 +0000
Last post2011-11-21 11:43 -0800
Articles 6 — 6 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Check if String.matches() AND (if yes) extract number from String? jochen2@brenz.com (Jochen Brenzlinger) - 2011-11-21 15:04 +0000
    Re: Check if String.matches() AND (if yes) extract number from String? Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-11-21 07:33 -0800
    Re: Check if String.matches() AND (if yes) extract number from String? Tassilo Horn <tassilo@member.fsf.org> - 2011-11-21 16:45 +0100
    Re: Check if String.matches() AND (if yes) extract number from String? Henk van Voorthuijsen <voorth@xs4all.nl> - 2011-11-21 07:39 -0800
    Re: Check if String.matches() AND (if yes) extract number from String? Roedy Green <see_website@mindprod.com.invalid> - 2011-11-21 10:26 -0800
    Re: Check if String.matches() AND (if yes) extract number from String? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-11-21 11:43 -0800

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

Fromjochen2@brenz.com (Jochen Brenzlinger)
Date2011-11-21 15:04 +0000
SubjectCheck if String.matches() AND (if yes) extract number from String?
Message-ID<4eca6890$0$6569$9b4e6d93@newsspool3.arcor-online.net>
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

[toc] | [next] | [standalone]


#10149

FromPeter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com>
Date2011-11-21 07:33 -0800
Message-ID<G9qdnRszctfF8lfTnZ2dnUVZ_q2dnZ2d@posted.palinacquisition>
In reply to#10148
On 11/21/11 7:04 AM, Jochen Brenzlinger wrote:
> 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?

I believe that if you want detailed information about the actual 
matches, you need to use either the Scanner or Matcher class, rather 
than the limited regex functionality of the String class.
http://download.oracle.com/javase/6/docs/api/java/util/Scanner.html
http://download.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html

I'm more accustomed to .NET regex handling, but either of those classes 
appear to provide the same match-group identification features you are 
looking for and which are available in .NET's Regex class.

Pete

[toc] | [prev] | [next] | [standalone]


#10150

FromTassilo Horn <tassilo@member.fsf.org>
Date2011-11-21 16:45 +0100
Message-ID<87ty5xlbrf.fsf@tsdh.uni-koblenz.de>
In reply to#10148
jochen2@brenz.com (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

[toc] | [prev] | [next] | [standalone]


#10151

FromHenk van Voorthuijsen <voorth@xs4all.nl>
Date2011-11-21 07:39 -0800
Message-ID<c17f9762-3174-42ba-88df-459f0c889341@j10g2000vbe.googlegroups.com>
In reply to#10148
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"));
}

[toc] | [prev] | [next] | [standalone]


#10158

FromRoedy Green <see_website@mindprod.com.invalid>
Date2011-11-21 10:26 -0800
Message-ID<3u5lc7hcdftcp6bcs5lo1sn8dtaj9mk1i8@4ax.com>
In reply to#10148
On 21 Nov 2011 15:04:48 GMT, jochen2@brenz.com (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. 

[toc] | [prev] | [next] | [standalone]


#10162

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2011-11-21 11:43 -0800
Message-ID<_Rxyq.24314$am1.10438@newsfe05.iad>
In reply to#10148
On 11/21/11 11:36 AM, Stefan Ram wrote:
> jochen2@brenz.com (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+)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web