[英]Regex to match 2 or more commas
我正在嘗試編寫一個正則表達式,用於識別字符串是否包含2個或更多連續逗號。 例如:
hello,,457
,,,,,
dog,,,elephant,,,,,
任何人都可以幫助有效的正則表達式嗎?
String str ="hello,,,457";
Pattern pat = Pattern.compile("[,]{2,}");
Matcher matcher = pat.matcher(str);
if(matcher.find()){
System.out.println("contains 2 or more commas");
}
以下正則表達式將匹配具有兩個或更多連續逗號的字符串,
^.*?,,+.*$
使用帶有matches
方法的正則表達式時,您不需要包含開始和結束錨點。
System.out.println("dog,,,elephant,,,,,".matches(".*?,,+.*"));
輸出:
true
嘗試:
int occurance = StringUtils.countOccurrencesOf("dog,,,elephant,,,,,", ",,");
要么
int count = StringUtils.countMatches("dog,,,elephant,,,,,", ",,");
取決於您使用的庫:在此處檢查解決方案: Java:如何計算String中char的出現次數?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.