2012-04-24 135 views
0

我需要解析此字符串並從中提取粗體數字。 我沒有爲 1. useInput合適的正則表達式可以是 = 「0067711990999999 * * 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程序。

感謝

+0

在這種情況下,什麼是「大膽」數字? – claesv 2012-04-24 06:51:19

+0

一年以上** 1955 *和溫度* 23 *。 – 2012-04-24 06:55:29

+1

字體更改本身不是字符。也許你原始形式的輸入有某種順序,表示字體改變;你可以確定它的順序和匹配。請注意,如果這個序列是HTML,那麼解析可能會出錯;除了匹配HTML標籤的常見問題之外,您可能需要擔心CSS。 – geekosaur 2012-04-24 06:59:06

回答

0

如果您正在閱讀.odt文件,或許http://www.jopendocument.org/是要走的路?

+0

ok對於odt它是有用的,但.log文件我會有問題。但無論如何,謝謝。 – 2012-04-25 06:59:07

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 

這就是你想要的?

+0

是的。但事情是在輸入* 1995 *和* 23 *的意思是粗體。 *不包含在字符串中,我正在從日誌文件和.odt文件中讀取這些文件。 – 2012-04-24 07:05:51

+0

好的,我不確定正則表達式可以幫助你。請參閱上面的@geekosaur的評論。請更新您的問題,以包含一個_actual_ userInput字符串,從日誌文件/ .odt文件中讀取。 – claesv 2012-04-24 07:07:49

+0

好的。輸入日誌爲006779195005150704 .................. 999999999999N9 + 00031 + 999999999 ....... 006771199991954051507004 .............. .... 999999999999N9 + 00111 + 999999999 ....... 00677119909999991955051507004 .................. 999999999999N9....... 00677119909999991956051507004 .................. 999999999999N9 + 00121 + 999999999 ....... 00677119909999991957051507004 .................. 999999999999N9 + 00121 + 999999999 ....... 00677119909999991954051507004 .................. 999999999999N9 + 00511 + 999999999 ....... 00677119909999991954051507004 .... .............. 999999999999N9 + 00111 + 999999999 – 2012-04-24 08:47:35