1
對於正則表達式並不熟悉,並使用以下代碼來查找單詞是否在結尾/開始處包含特殊字符。如何查找和跳過單詞開頭和結尾的特殊字符
String s = "K-factor:";
String regExp = "^[^<>{}\"/|;:.,[email protected]#$%^=&*\\]\\\\()\\[0-9_+]*$";
Matcher matcher = Pattern.compile(regExp).matcher(s);
while (matcher.find()) {
System.out.println("Start: "+ matcher.start());
System.out.println("End: "+ matcher.end());
System.out.println("Group: "+ matcher.group());
s = s.substring(0, matcher.start());
}
想要在字符串的開始或結束處查找是否有任何特殊字符(:在此示例代碼中)。試圖跳過角色。 既不編譯時間錯誤也不輸出。
如果在開始和結束時有字母以外的其他字符,您想匹配嗎?然後使用'「^ \\ P {L} | \\ P {L} $」'。你的正則表達式與'K因子'不匹配,因爲它最後包含':'。 –
Thanks @WiktorStribiżew工作得很好。請張貼答案,以便其他人可以參考。 – srikanth