Use multiple regular expressions to check for blacklists in a string

I have a string with a value. I want to check if there is a reverse list on this line.

ex: String myString="a/b[c=\"1\"=\"1\"]/c\^]

I want to check the following patterns there

  • "1" = "1"
  • ^

I use the following code that always gives false

         String text    = "\"1\"=\"1\" ^ for occurrences of the http:// pattern.";

        String patternString = "\"1\"=\"1\"|^";

        Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);

        Matcher matcher = pattern.matcher(text);

        boolean matches = matcher.matches();

        System.out.println("matches = " + matches)

How can I test it with a single line of regular expression.

+4
source share
2 answers

A couple of problems with your code:

String patternString = "\"1\"=\"1\"|^";

It ^should be escaped here, as it ^is a special metacharacter, so do this:

String patternString = "\"1\"=\"1\"|\\^";

Then this call:

boolean matches = matcher.matches();

should be changed to:

boolean matches = matcher.find();

as matchestries to match the complete input line.

+2
source

, BOTH "1"="1" ^

:

String text    = "\"1\"=\"1\"  ^ for occurrences of the http:// pattern.";
Pattern p = Pattern.compile("\"1\"=\"1\".*\\^|\\^.*\"1\"=\"1\"");
Matcher m = p.matcher(text);
if(m.find())
    System.out.println("Correct String");

contains:

String text    = "\"1\"=\"1\"  ^ for occurrences of the http:// pattern.";
if (text.contains("\"1\"=\"1\"") && text.contains("^"))
        System.out.println("Correct String");
+1

Source: https://habr.com/ru/post/1589307/


All Articles