[英]Java Regex - need help on number match
我正在嘗試使用正則表達式匹配字符串24-15-7-49-63-2
中的所有值1
到18
。 我以前使用regex做一般用途,但我不知道該怎么做。
棘手的是,您無法輕松地使用正則表達式定義范圍。 但這可能會滿足您的要求:
\b([1-9]|1[0-8])\b
您可以在此處查看其運行情況: http : //regexr.com?2v8jj
這是java中的示例:
String text = "24-15-7-49-63-2";
String pattern = "\\b([1-9]|1[0-8])\\b";
Pattern compiledPattern = Pattern.compile(pattern);
Matcher matcher = compiledPattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
輸出:
15
7
2
編輯:根據注釋,您可以使用以下模式獲得唯一的匹配項:
\b([1-9]|1[0-8])\b(?!.*\b\1\b.*)
實際使用中: http : //regexr.com?2v8kh
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.