[英]Regex combination
正則表達式中是否可以合並兩個驗證? 每當在語句中給定一系列8位數字(12345678)以及空格(1234 5678)時,Regex應該引發錯誤。 以及對字母和其他特殊字符的驗證。
例如:1234公寓,#23建築狀態234456(有效一)
1234套間,#23建築狀態1234 5678超車(由於8位數字的連續/連續,因此無效)
677樓,@(23)樓,向上狀態56789123,英國狀態(由於連續8位數字而無效)
(?:\d *){8}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
final String regex = "(?:\\d *){8}";
final String[] strings = {
"1234 offshore address1234 Some locations",
"1234 5678 offshore some location"
};
final Pattern pattern = Pattern.compile(regex);
for (String s: strings) {
Matcher matcher = pattern.matcher(s);
if(!matcher.find()) {
System.out.println(s);
}
}
}
}
1234 offshore address1234 Some locations
1234 5678 offshore some location
下面顯示了不匹配的字符串。
1234 offshore address1234 Some locations
(?:\\d *){8}
匹配一個數字,后跟任意數量的空格,精確匹配8次
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.