1

I saw plenty of questions regarding problem with replaceAll but it didn't work for me. I would like to replace occurance of particular pattern with pattern itself (so later on I will be able to run sql and find similiar records based on regex value) by given:

https://internal-gateway.com/users/4e8a4741-dd89-4cdd-a7c3-3b2f7044e142

it shoud return

https://internal-gateway.com/users/[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89AB][0-9a-f]{3}-[0-9a-f]{12}

my code:

private final static String UUID_PATTERN = "[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89AB][0-9a-f]{3}-[0-9a-f]{12}";
private final static String SIM_NUMBER_PATTERN = "894803[0-9]{13}";
private final static String MSISDN_SHORT_PATTERN = "[0-9]{9}";
private final static String MSISDN_LONG_PATTERN = "[0-9]{11}";
private Optional<WebApp> checkRegexes(String url, Long id) {
    String urlToLookFor = url.replaceAll(UUID_PATTERN,UUID_PATTERN);
    urlToLookFor=urlToLookFor.replaceAll(SIM_NUMBER_PATTERN,SIM_NUMBER_PATTERN);
    urlToLookFor=urlToLookFor.replaceAll(MSISDN_LONG_PATTERN,MSISDN_LONG_PATTERN);
    urlToLookFor=urlToLookFor.replaceAll(MSISDN_SHORT_PATTERN,MSISDN_SHORT_PATTERN);

    return waRepository.getWebAppByRegex(urlToLookFor,id);
}

however in given example value replaceall is not replacing anything, can anyone give me a hint what is my mistake?

2
  • 1
    Your pattern doesn't match your value Commented Oct 14, 2019 at 11:27
  • Your pattern doesn't match the value : regex101 Commented Oct 14, 2019 at 11:28

1 Answer 1

4

String.replaceAll() is case-sensitive, and your pattern doesn't match your string here:

[89AB][0-9a-f]{3}: "AB" is upper case

a7c3: "a" is lower case

Just replace [89AB] with [89ab], or use a case-insensitive pattern matching method.

Sign up to request clarification or add additional context in comments.

1 Comment

Didn't noticed that my regex testing plugin has disabled case sensitive ;/ thanks for pointing this out, it looks like adding (?i) before pattern in replaceAll worked !

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.