Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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,我想验证字符串模式。如果字符串中没有任何特殊字符,它将与下面的代码一起工作 例如: 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 regist

我想验证字符串模式。如果字符串中没有任何特殊字符,它将与下面的代码一起工作

例如:

    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");
} 
(、)和。在正则表达式中使用。如果你想要正常的行为,你需要避开它们。

试试这个:

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");
} 

(、)和。在正则表达式中使用。如果您想要正常的行为,您需要对其进行转义。

在正则表达式中,当您需要逐字匹配特殊字符时,必须始终注意它们

在本例中,您有3个字符:
(用于打开组)、
(用于关闭组)和
(匹配任何字符)

要从字面上匹配它们,您需要对它们进行转义,或者将它们放入字符类
[…]


请参见regex中的

,当您需要逐字匹配特殊字符时,必须始终注意它们

在本例中,您有3个字符:
(用于打开组)、
(用于关闭组)和
(匹配任何字符)

要从字面上匹配它们,您需要对它们进行转义,或者将它们放入字符类
[…]

}

输出:真 真的 假的 假的

}

输出:真 真的 假的
false

转义括号和点。在replaceAll()方法中,转义括号和点“*”替换为“*”。“*”使用此模式匹配任何数字、任何字符的任何字符串(包括空字符串)。“*”在replaceAll()方法中替换为“*”。“*”使用此模式匹配任何数字、任何字符的任何字符串(包括空字符串)。
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

}