我需要编写仅允许数字,&&之类的正则表达式 。()和空格。
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
List<String> input = new ArrayList<String>();
input.add("(0.4545 && 0.567) || 456"); // should PASS
input.add("9876-5-4321");
input.add("987-65-4321 (attack)");
input.add("(0.4545 && 0.567) || 456 && (me)");
input.add("0.456 && 0.567"); // should PASS
for (String ssn : input) {
boolean f = ssn.matches("^[\\d\\s()&|.]$");
if (f) {
System.out.println("Found good SSN: " + ssn);
}else {
System.out.println("Nope: " + ssn);
}
}
}
}
以上都没有通过,为什么?
您忘记+
在角色课后添加。没有它,您的正则表达式将只接受带有字符类中字符的单字符字符串。试试看
boolean f = ssn.matches("^[\\d\\s()&|.]+$");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句