Zuriar
Zuriar

Reputation: 11734

Regex to find whole word in text but case insensitive

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.

Upvotes: 33

Views: 94619

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174696

Use word boundary \b in your regex,

(?i)\brum\b

OR

Use lookahead and lookbehind,

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

DEMO

Upvotes: 63

Related Questions