user1631306
user1631306

Reputation: 4470

Extract number from within a string

I know its been answered already, but I am struggling to extract the number from the given string

internal-pdf://2586543536/Homan-2014-RNA tertiary structure analysis by.pd

I need to extract "2586543536"

I am using following regex, which I am sure is incorrect.

Pattern p = Pattern.compile("internal-pdf://\\d+");
Matcher m = p.matcher(value);
System.out.println(m.group(1));

Upvotes: 1

Views: 140

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627507

You need to wrap \d+ with a capturing group and run the matcher with m.find() to find a partial match:

String value = "internal-pdf://2586543536/Homan-2014-RNA tertiary structure analysis by.pd";
Pattern p = Pattern.compile("internal-pdf://(\\d+)");
Matcher m = p.matcher(value);
if (m.find()){
    System.out.println(m.group(1)); 
} 

See the Java demo.

Upvotes: 5

Related Questions