1

I have input string which I want to convert to regex and then compare against another string.

String inputString = "https://*.mlc.net/Gateway/api/Transmissions/State (body 0 bytes)";

String strToMatch = "https://abc.mlc.net/Gateway/api/Transmissions/State (body 0 bytes)";

I have done it like:

String regex = inputString.replace("*", "(.*)");

strToMatch.matches(regex) 

but it fails. Can someone please explain the code?

I know backslashes are required before paranthesis but would require code which will work for all characters in the input string.

2
  • 1
    You also need to escape the characters that have a meaning in a regexp. Commented Feb 18, 2019 at 9:27
  • In your input string '(' and ')' should be escaped. inputString = "https://*.mlc.net/Gateway/api/Transmissions/State \\(body 0 bytes\\)"; should work for the given example. Commented Feb 18, 2019 at 9:36

2 Answers 2

2

Your regex doesn't match because there are some regex metacharacters in your input string. Namely, the regex engine interprets the parentheses around body 0 bytes as a capture group instead of literal parentheses. You want everything in the input string to be matched literally, except the (.*) parts, right?

You can split the input string with *, then for each part, call Pattern.quote to escape all the regex meta characters. Finally, you join the parts together with (.*):

private static String createRegex(String inputString) {
    String[] parts = inputString.split("\\*");
    return Arrays.stream(parts).map(Pattern::quote).collect(Collectors.joining("(.*)"));
}
Sign up to request clarification or add additional context in comments.

4 Comments

Right, but can you please confirm if this will work for all characters in the input string which have meaning in regex?
@user3770039 Yes it will, according to the docs.
Thanks, will this work if input string does not contain *?
@user3770039 Yes, it will create a regex that matches exactly the input string.
1

You also need to escape the characters that have a meaning in a regexp:

    String regex = formatRe(inputString);

...

private static String formatRe(String inputString) {
    StringBuilder buf = new StringBuilder();
    for (char c: inputString.toCharArray()) {
        switch (c) {
            case '*':
                buf.append("(.*)");
                break;
            case '.':
            case '(':
            case ')':
            case '[':
            case ']':
                // ...
                buf.append('\\');
                buf.append(c);
                break;
            case '\t':
                buf.append("\\t");
                break;
            default:
                buf.append(c);
                break;
        }
    }
    return buf.toString();
}

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.