Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to find whole word in text but case insensitive

Tags:

regex

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.

like image 253
Zuriar Avatar asked Aug 25 '14 09:08

Zuriar


People also ask

How do you make a regex match not case-sensitive?

Case-Sensitive Match To disable case-sensitive matching for regexp , use the 'ignorecase' option.

Is regex search case-sensitive?

In Java, by default, the regular expression (regex) matching is case sensitive.

What does \b mean in regex?

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.

How do you match a whole expression in regex?

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.


1 Answers

Use word boundary \b in your regex,

(?i)\brum\b

OR

Use lookahead and lookbehind,

(?i)(?<= |^)rum(?= |$)

DEMO

like image 127
Avinash Raj Avatar answered Oct 16 '22 17:10

Avinash Raj