我想验证字符串模式。如果字符串中没有任何特殊字符,则使用下面的代码。
例如:
Pattern p = Pattern.compile("Dear User, .* is your One Time Password(OTP) for registration.",Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("Dear User, 999 is your One Time Password(OTP) for registration.");
if (m.matches()){
System.out.println("truee");
}else{
System.out.println("false");
} // output false
如果我删除(和),及以下工作正常。
Pattern p = Pattern.compile("Dear User, .* is your One Time Password OTP for registration.",Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("Dear User, 999 is your One Time Password OTP for registration.");
if (m.matches()){
System.out.println("truee");
}else{
System.out.println("false");
} // output true
答案 0 :(得分:0)
试试这个:
d[!is.finite(d)] <- NA
(,)和。用于正则表达式。如果你想要正常的行为,你需要逃脱它们。
答案 1 :(得分:0)
在正则表达式中,当你需要匹配字面时,你必须始终注意特殊字符。
在这种情况下,您有3个字符:(
(用于打开组),)
(用于关闭组)和.
(匹配任何字符)。< / p>
要按字面意思匹配它们,您需要将它们转义,或放入字符类[...]
。
请参阅fixed demo
答案 2 :(得分:0)
package com.ramesh.test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PatternMatcher {
public boolean containsPattern(String input, String pattern) {
if (input == null || input.trim().isEmpty()) {
System.out.println("Incorrect format of string");
return false;
}
// “ * ”is replaced by “ .* ” in replaceAll()
//“ .* ” Use this pattern to match any number, any string (including the empty string) of any characters.
String inputpattern = pattern.replaceAll("\\*", ".*");
System.out.println("first input" + inputpattern);
Pattern p = Pattern.compile(inputpattern);
Matcher m = p.matcher(input);
boolean b = m.matches();
return b;
}
public boolean containsPatternNot(String input1, String pattern1) {
return (containsPattern(input1, pattern1) == true ? false : true);
}
public static void main(String[] args) {
PatternMatcher m1 = new PatternMatcher ();
boolean a = m1.containsPattern("ma5&*%u&^()k5.r5gh^", "m*u*r*");
System.out.println(a);// returns True
boolean d = m1.containsPattern("mur", "m*u*r*");
System.out.println(d);// returns True
boolean c = m1.containsPatternNot("ma5&^%u&^()k56r5gh^", "m*u*r*");
System.out.println(c);// returns false
boolean e = m1.containsPatternNot("mur", "m*u*r*");
System.out.println(e);// returns false
}
}
输出:true 真正 假 假