Reputation: 2353
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?
Upvotes: 1
Views: 58
Reputation: 2303
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.
Upvotes: 4