Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java正则表达式组合表达式_Java_Regex - Fatal编程技术网

Java正则表达式组合表达式

Java正则表达式组合表达式,java,regex,Java,Regex,我正在尝试编写使用正则表达式返回信用卡供应商的代码,这似乎是可行的,例如: // Visa - always begins digit 4, 13 or 16 digits long ^[4].{12}|^[4].{15} // Visa Electron - begin with the code pattern, 16 digits long 4026.{12}|417500.{10}|4405.{12}|4508.{12}|4844.{12}|4913.{12}|4917.{12}

我正在尝试编写使用正则表达式返回信用卡供应商的代码,这似乎是可行的,例如:

// Visa - always begins digit 4, 13 or 16 digits long
^[4].{12}|^[4].{15} 

// Visa Electron - begin with the code pattern, 16 digits long
4026.{12}|417500.{10}|4405.{12}|4508.{12}|4844.{12}|4913.{12}|4917.{12}
因此,对于isVisa方法,我希望正则表达式表示“基于Visa正则表达式返回Visa,但不基于Visa Electron”

此代码不起作用:

    public static String isVisa(String number) {
    Pattern p = Pattern.compile("^[4].{12}|^[4].{15}&&(!4026.{12}|!417500.{10}|!4405.{12}|!4508.{12}|!4844.{12}|!4913.{12}|!4917.{12})");
    Matcher m = p.matcher(number);
    if (m.matches()) return "Visa";
    else return "";
}

使用正数和负数lookahead检查以某些数字开头而不是以某些特定数字开头的行

^(?!417500.{10}|4026.{12}|4405.{12}|4508.{12}|4844.{12}|4913.{12}|4917.{12})(?=4.{12}|4.{15}).*

该方法根据整个字符串验证正则表达式,因此不需要开始
^
和结束
$
锚定。此外,正则表达式引擎正在匹配这些字符
&
作为文本,您似乎正在尝试将其用作运算符

要忽略这些模式,您可以使用负前瞻来实现这一点

(?!.*(?:(?:4026|4405|4508|4844|4913|4917).{12}|417500.{10}))(?=4.{12}|4.{15}).*
示例:这将返回前两个的
true
,因为其余的对您的情况无效

String[] numbers = { "4000236512341234", "4000222213232222", "4026122222222222", 
                     "4175000000000000", "4405343434344343", "4508111111111111",
                     "4844000000000000", "4913000000000000", "4917000000000000" };

Pattern p = Pattern.compile("(?!.*(?:(?:4026|4405|4508|4844|4913|4917).{12}|417500.{10}))(?=4.{12}|4.{15}).*");

for (String x: numbers) {
    Matcher m = p.matcher(x);
    System.out.println(m.matches());
}
输出

true
true
false
false
false
false
false
false
false

您可以使用非常简单易懂的正则表达式:

4026.{12}|417500.{10}|4405.{12}|4508.{12}|4844.{12}|4913.{12}|4917.{12}|([4].{12}|[4].{15})
这将匹配所有Visa Electron模式,而不会捕获任何内容。当您获得匹配时,请检查捕获组1是否为非
null
,以确定它是否为Visa


有关该技术的详细说明,请参阅。

FYI您需要长度16-19,Visa与Eletcron在许多范围内重叠,要正确执行此操作,您需要定期更新BIN范围的查找表(我上周也在做类似的事情),最后一行对
return m.matches()不是更好吗?“签证”:“对我来说,这似乎是一个布尔函数。您可能希望在此处返回布尔值,而不是字符串