从字符串中提取数字

时间:2017-01-04 14:35:14

标签: java regex

我知道它已经被回答了,但是我很难从给定的字符串中提取数字

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

我需要提取“2586543536”

我正在使用以下正则表达式,我相信这是不正确的。

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

1 个答案:

答案 0 :(得分:5)

您需要使用捕获组包装\d+并使用m.find()运行匹配器以查找部分匹配:

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)); 
} 

请参阅Java demo

相关问题