下面的代碼
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
這就是你想要的?
在這種情況下,什麼是「大膽」數字? – claesv 2012-04-24 06:51:19
一年以上** 1955 *和溫度* 23 *。 – 2012-04-24 06:55:29
字體更改本身不是字符。也許你原始形式的輸入有某種順序,表示字體改變;你可以確定它的順序和匹配。請注意,如果這個序列是HTML,那麼解析可能會出錯;除了匹配HTML標籤的常見問題之外,您可能需要擔心CSS。 – geekosaur 2012-04-24 06:59:06