模式匹配识别字符串中的粗体字符

时间:2012-04-24 06:43:18

标签: java regex

我需要解析这个字符串并提取粗体数字。 我没有合适的正则表达式  1. useInput可以     = “0067711990999999 * 1955 * 0515070999999999999N9 + 01 * 23 * 1 + 99999999 ..”;

Pattern pattern;
    String regex="\\s*-?\\d+(?:\\s*[-+/*]\\s*-?\\d+)+\\s*";
    pattern=Pattern.compile(regex);
    Matcher match = pattern.matcher(userInput);

问题是我找不到任何匹配粗体字符串的正则表达式。

我需要在Map-Reduce程序中使用它。

由于

2 个答案:

答案 0 :(得分:0)

以下代码

String myString = "0067711990999999*1955*0515070999999999999N9+01*23*1+99999999";
// matches all number series (one or more consecutive digits) 
// between * characters. * normally matches any character and 
// so has to be escaped using \, which in a string becomes \\,
// i.e the regular expression is actually \*([0-9])\*
Pattern pattern = Pattern.compile("\\*([0-9]+)\\*");
Matcher matcher = pattern.matcher(myString);
while (matcher.find()) {
    // the parantheses in the regex creates a capturing group, i.e.
    // a substring within the match that can later be extracted. 
    // the "1" here means we're picking up the value of the first 
    // (and in this case, only) capturing group, which is the 
    // actual numbers (i.e. not including the * characters)
    System.out.println(matcher.group(1));
}

会打印

1955
23

这就是你要找的东西吗?

答案 1 :(得分:0)

如果您正在阅读.odt文件,或许可以使用http://www.jopendocument.org/吗?