我想在我的java程序中使用正則表達式來識別我的字符串的某些功能。 我有這種類型的字符串:複雜的正則表達式
`-Author-已經寫了(-hh - : - MM-)
因此,舉例來說,我有一個字符串:
切科已經寫了(15:12)
,我已經提取作者,HH和MM領域。很顯然,我已經有一定的約束考慮:
hh and mm must be numbers
author hasn't any restrictions
I've to consider space between "has wrote" and (
我不知道我該如何使用正則表達式,你能幫幫我嗎?
編輯:我附上我的片斷:
String mRegex = "(\\s)+ has wrote \\((\\d\\d):(\\d\\d)\\)";
Pattern mPattern = Pattern.compile(mRegex);
String[] str = {
"Cecco CQ has wrote (14:55)", //OK (matched)
"yesterday you has wrote that I'm crazy", //NO (different text)
"Simon has wrote (yesterday)", // NO (yesterday isn't numbers)
"John has wrote (22:32)", //OK
"James has wrote(22:11)", //NO (missed space between has wrote and()
"Tommy has wrote (xx:ss)" //NO (xx and ss aren't numbers)
};
for(String s : str) {
Matcher mMatcher = mPattern.matcher(s);
while (mMatcher.find()) {
System.out.println(mMatcher.group());
}
}
找到「已經寫了」?你會想要放棄「有」 - 增加的好處:「你」和「我」也將除了實際的名字工作。 – 2011-01-20 10:46:58