2013年1月21日 星期一
Java 正規表示範例
import java.util.regex.Matcher;
import java.util.regex.Pattern;
---------------------------------------------------------------------------------------
public void parserCircuitID(String inputStr) {
String patternStr = "circuit [0-9]+/[0-9]+:[0-9]+:[0-9]+/[0-9]+/[0-9]+/[0-9]+";
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(inputStr);
boolean matchFound = matcher.find();
System.out.print(matchFound);
while (matchFound) { //有符合項
System.out.println(matcher.start() + "-" + matcher.end());
for (int i = 0; i <= matcher.groupCount(); i++) {
String groupStr = matcher.group(i); //正規表示式若有括號則依序取
System.out.println(i + ":" + groupStr);
}
if (matcher.end() + 1 <= inputStr.length()) { //未到結尾並且還有符合項
matchFound = matcher.find(matcher.end());
} else {
break;
}
}
}
訂閱:
文章 (Atom)