0

I have the following string:

String s = "http://www.[VP_ANY].com:8080/servlet/[VP_ALL]";

I need to check if this string has the words [VP_ANY] o [VP_ALL]. I tried something like this (and many combinations), but it doesn't work:

Pattern.compile("\b(\\\\[VP_ANY\\\\]|\\\\[VP_ALL\\\\])\b").matcher(s).matches()

What am I doing wrong?

I tried the following:

s = "www.[VP_ANY].com:8080/servlet/[VP_ALL]";

System.out.println(Pattern.compile("\[VP_ANY\]").matcher(s).matches());

System.out.println(s.replaceAll("\[VP_ANY\]", "A"));

The first 'System.out' returns false, and the second one returns the replacement correctly.

I'm escaping the "[" and "]" characters with 2 backslashes, but when I save the post just one is showed. But I'm using 2 ...

3
  • 1
    Should both words be present? In the given order? Why did you write "replace two words" in the title? What should they be replaced by? Commented May 22, 2012 at 12:56
  • You are searching for a literal [VP_ANY] and ~ALL? Commented May 22, 2012 at 13:02
  • Sorry for the title, I only need to know if any of the words are present, order is not important Commented May 22, 2012 at 13:07

4 Answers 4

1
Pattern.compile("\b(\\\\[VP_ANY\\\\]|\\\\[VP_ALL\\\\])\b").matcher(s).matches()
String s = "http://www.[VP_ANY].com:8080/servlet/[VP_ALL]";
                      ^^      ^^                ^^      ^
                      NoWB    NoWB              NoWB    WB

Your regex will not work because there is no word boundaray between . and [, between ] and . and between / and [

Additionally I think you are wrong with the escaping, your word boundaries would need a backslash more and the others two less.

So, since the word boundaries are not working, you should be fine with

Pattern.compile("\\[VP_(?:ANY|ALL)\\])")
Sign up to request clarification or add additional context in comments.

Comments

0

Try this one

try {
    boolean foundMatch = subjectString.matches("(?i)\\bVP_(?:ANY|ALL)\\b");
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
}

or this

try {
    boolean foundMatch = subjectString.matches("(?i)\\[VP_(?:ANY|ALL)\\]");
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
}

1 Comment

String s = "www.[VP_ANY].com:8080/servlet/[VP_ALL]"; System.out.println(Pattern.compile("(?i)\\bVP_(?:ANY|ALL)\\b").matcher(s).matches()); ---> false
0

Try This

\[VP_ANY\]|\[VP_ALL\]

My go at Java

try {
    boolean foundMatch = "www.[VP_ANY].com:8080/servlet/[VP_ALL]".matches("\\[VP_ANY\\]|\\[VP_ALL\\]");
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
}

2 Comments

String s = "www.[VP_ANY].com:8080/servlet/[VP_ALL]"; System.out.println(Pattern.compile("\[VP_ANY\]|\[VP_ALL\]|").matcher(s).matches()); ---> false
@JBalaguero The regex is good. I think your java code is not what it should be. I update my answer with my limited knowledge on how to do it in Java
0
"http://www.[VP_ANY].com:8080/servlet/[VP_ALL]".replaceAll ("http://www.(\\[VP_ANY\\]).com:8080/servlet/(\\[VP_ALL\\])", "$1:$2")
res117: java.lang.String = [VP_ANY]:[VP_ALL]

If you're looking for a literal [, you have to mask it - else it will mean a group like [A-Z].

Now if you read the regex from a file or a JTextField at runtime, that's all. But if you write it to your source code, the compiler will see the \ and treat it as a general masking, which might be needed to mask quotes like in

char apo = '\''; 
String quote = "He said: \"Whut?\"";

So you have to mask it again, because only "\\" means "\".

So, for development, to not get too much confused, it is a fine idea to have a simple GUI-App with 2 or 3 textfields for testing regexps. If you succeed, you only have to add another level of masking, but to develop them, you can keep this second level away.

Divide et impera, like the ancient roman programmers told us.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.