Frederik Witte
Frederik Witte

Reputation: 1345

Java(Apex) RegEx not working?

I am having trouble with a regex in salesforce, apex. As I saw that apex is using the same syntax and logic as apex, I aimed this at java developers also. I debugged the String and it is correct. street equals 'str 3 B'.

When using http://www.regexr.com/, the regex works('\d \w$').

The code:

Matcher hasString = Pattern.compile('\\d \\w$').matcher(street);
    if(hasString.matches())

My problem is, that hasString.matches() resolves to false. Can anyone tell me if I did something somewhere wrong? I tried to use it without the $, with difference casing, etc. and I just can't get it to work.

Thanks in advance!

Upvotes: 2

Views: 718

Answers (1)

anubhava
anubhava

Reputation: 785481

You need to use find instead of matches for partial input match as matches attempts to match complete input text.

Matcher hasString = Pattern.compile("\\d \\w$").matcher(street);
if(hasString.find()) {
   // matched
   System.out.println("Start position: " + hasString.start());
}

Upvotes: 3

Related Questions