Skip to content Skip to sidebar Skip to footer

Matching A Word With Pound (#) Symbol In A Regex

I have regexp for check if some text containing word (with ignoring boundary) String regexp = '.*\\bSOME_WORD_HERE\\b.*'; but this regexp return false when 'SOME_WORD' starts with

Solution 1:

The \b# pattern matches a # that is preceded with a word character: a letter, digit or underscore.

If you need to match # that is not preceded with a word char, use a negative lookbehind (?<!\w). Similarly, to make sure the trailing \b matches if a non-word char is there, use (?!\w) negative lookahead:

text.matches("(?s).*(?<!\\w)" + matchingWord + "(?!\\w).*");

Using Pattern.quote(matchingWord) is a good idea if your matchingWord can contain special regex metacharacters.

Alternatively, if you plan to match your search words in between whitespace or start/end of string, you can use (?<!\S) as the initial boundary and (?!\S) as the trailing one

text.matches("(?s).*(?<!\\S)" + matchingWord + "(?!\\S).*");

And one more thing: the .* in the .matches is not the best regex solution. A regex like "(?<!\\S)" + matchingWord + "(?!\\S)" with Matcher#find() will be processed in a much more optimized way, but you will need to initialize the Matcher object for that.

Solution 2:

If you are looking for words with leading '#', just simple remove the leading '#' from the searchword and use following regex.

text.matches("#\\b"+ matchingWordWithoutLeadingHash +"\\b");

Post a Comment for "Matching A Word With Pound (#) Symbol In A Regex"