Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Check if String.matches() AND (if yes) extract number from String? (http://www.velocityreviews.com/forums/t806154-check-if-string-matches-and-if-yes-extract-number-from-string.html)

Jochen Brenzlinger 11-21-2011 03:04 PM

Check if String.matches() AND (if yes) extract number from String?
 
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


Henk van Voorthuijsen 11-21-2011 03:39 PM

Re: Check if String.matches() AND (if yes) extract number from String?
 
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"));
}


Tassilo Horn 11-21-2011 03:45 PM

Re: Check if String.matches() AND (if yes) extract number from String?
 
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

Roedy Green 11-21-2011 06:26 PM

Re: Check if String.matches() AND (if yes) extract number from String?
 
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.

Stefan Ram 11-21-2011 07:36 PM

Re: Check if String.matches() AND (if yes) extract number from String?
 
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" ))


Daniel Pitts 11-21-2011 07:43 PM

Re: Check if String.matches() AND (if yes) extract number from String?
 
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+)


All times are GMT. The time now is 08:35 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.