2

I need to find specific word on a string using regular expression. rules : before that word, I must not have a letter, a number or an underscore. this word must be not followed by a letter, a number or an underscore.

Example : I'm looking for the word 'the' in : 'Hi, it\'s a random text containing some words, the .the thingthe THE, tthe Tthe [email protected]'

in this string It must be found 3 times : the and .the and THE so I must get true as a return.

my code :

String str = 'Hi, it\'s a random text containing some words, the .the thingthe THE, tthe Tthe [email protected]';
String var = 'the';
String s = '(?<![\\w])'+var.toUpperCase()+'(?![\\w])';
Pattern MyPattern = Pattern.compile(s);
Matcher MyMatcher = MyPattern.matcher(str.toUpperCase());
System.debug('###matches : '+MyMatcher.matches());

Matches return always false. I tried with this also : '(?<![a-zA-Z0-9_])'+var.toUpperCase()+'(?![a-zA-Z0-9_])';

2 Answers 2

5

The matches method will only return true if the entire input string matches the result. You want to use find instead, which will match a substring. Take a look at the Matcher documentation for these methods:

  • find()
    Attempts to find the next subsequence of the input sequence that matches the pattern. This method returns true if a subsequence of the input sequence matches this Matcher object's pattern.
  • matches()
    Attempts to match the entire region against the pattern.

So if you want to know whether your substring is contained anywhere in the input string, use MyMatcher.find() instead.

A couple side notes:

  • If you want your expression to be case insensitive, set the i flag by starting your expression with (?i).
  • If you want to look for a whole word, wrap it in the word boundary character (\b), not backward and forward lookaheads for non-word characters.
  • With the above in mind, I would change the expression to: '(?i)\\b' + var + '\\b'
0
0

before that word, I must not have a letter, a number or an underscore.

A /W is supposed to match any character that is not a word character (alphanumeric & underscore) and is equivalent to (^A-Za-z0-9_).

That having been said, I think at the root of your issue is that you're using square brackets [\\W] in your code when apex is expecting parens (\\W) for your capture groups.

And as @Adrian Larson noted, you do need to use MyMatcher.find().

6
  • Square brackets vs parens is definitely not the issue here. They're not trying to capture the surrounding non-word characters, they're trying to specifically ignore them. Also I think you got the wrong slash before your W. ;)
    – Adrian Larson
    Dec 22, 2016 at 15:54
  • I looked at his code earlier and initially started to discuss non capture groups. He's using negative look aheads and negative look behinds rather than non-capture groups which would look like (?:\\W) NOT his (?<![\\w]) and (?![\\w]) which would seem to also be a problem. That would prevent him from finding what his IS looking for. The first slash needs to be escaped is why there's two. I don't believe it to be a forward slash, but I'd have to look at my apex code. The references show it as a backslash at regexr.com
    – crmprogdev
    Dec 22, 2016 at 16:02
  • Yeah as you've basically noted yourself the brackets are entirely extraneous. In fact, I think having any type of capturing group or lookahead/lookbehind is completely extraneous. In this case there's no real difference between (?:\W) and \W.
    – Adrian Larson
    Dec 22, 2016 at 16:04
  • Yes, exactly my point. His look aheads and behinds are preventing him from finding what he's looking for. When I initially read his question, I thought he was trying to find out what the look ahead and behind should be to find what to exclude to reveal what he wanted to find.
    – crmprogdev
    Dec 22, 2016 at 16:09
  • Have you tried the initial expression? It does work with find. It's inefficient, perhaps, but not flat out wrong. The matcher from the OP will find three groups.
    – Adrian Larson
    Dec 22, 2016 at 16:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .