I have the following text:
I like a bit of rum with my crumble pie.
I want to build a regex search expression to return just the word 'rum' not 'crumble' as well. Also I need it to be case insensitive.
Case-Sensitive Match To disable case-sensitive matching for regexp , use the 'ignorecase' option.
In Java, by default, the regular expression (regex) matching is case sensitive.
A word boundary \b is a test, just like ^ and $ . When the regexp engine (program module that implements searching for regexps) comes across \b , it checks that the position in the string is a word boundary.
To run a “whole words only” search using a regular expression, simply place the word between two word boundaries, as we did with ‹ \bcat\b ›. The first ‹ \b › requires the ‹ c › to occur at the very start of the string, or after a nonword character.
Use word boundary \b
in your regex,
(?i)\brum\b
OR
Use lookahead and lookbehind,
(?i)(?<= |^)rum(?= |$)
DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With