请帮助开发正则表达式。我的要求是验证以下字符串,
String="asdasd|adajjsd|asas,asdas|asda|sd";
模式 id [String]separatedByPipe[String]separatedByPipe[String]
例如:String="asdasd|adajjsd|asas"
。
重复上述模式,用逗号分隔,如果有多个字符串 ex: String="asdasd|adajjsd|asas,asdas|asda|sd"
;
我可以假设该模式最多只会重复 3 次,即在我的字符串中最多重复 2 个逗号。
附上我的testClass
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestReg {
public static void main(String[] args) {
//String regex = "[a-z]+\\|[a-z]+,[a-z]+\\|[a-z]*";
String regex = "([a-z]\\|[a-z]\\|[a-z]\\,?)+";
//String regex = "[a-z]+\\|re{ 1}[a-z]+\\|re{ 1}[a-z]+[a-z]+\\|[a-z]+\\|[a-z]*";
//String regex = "[a-z](|)[a-z](,)[a-z](|)[a-z]";
//String pattern="([a-z]+@)([a-z]+)(\\.[a-z]+)";
//String inputString1 = "shdifhsdui-asdasd,shdifhsdui|abc";
//String inputString1 = "";//failed
String inputString1 = "asdasd|adajjsd|asas,asdas|asda|sd";//success
//String inputString1 = "asdasd|adasd-asdas|asdasd";//success
//String inputString1 = "asdasd|adasd|asassa,asdas|asdasd";//failed
Pattern pattern1 = Pattern.compile(regex);
Matcher matcher1 = pattern1.matcher(inputString1);
boolean result = matcher1.find();
System.out.println(result);
}
}
大家好 ,
System.out.println ("asdasd|adajjsd|asa,s|sad|sdas,asd|as|as|da".matches ("([a-z]*\\|[a-z]*\\|[a-z]*\\,?)+"));
像魅力一样工作。那么为什么在我上面的课程中同样不起作用。请帮助。我正在使用 jdk1.5