How to check a pattern without case sensitivity only for one portion in a string?
We will add (?i)
for matching a specific portion of a string in case insensitive way. Let’s see an example –
We have one string below :
Hello World
We want to check Hello without case sensitivity and World as it is, means with case sensitivity.
Regex will be
((?i)hello)\sWorld
Output :
Hello World
Expression Explanation :
( : Represent the starting of captured group.
(?i) : Case-insensitive flag.
hello : Matches the word “Hello“
) : Represent the ending of captured group.
\s : Matches any whitespace character like space, tab, newline etc.
World : Matches the word “World“.
In the captured group ((?i)hello)
we have written hello instead of Hello for showing how the case insensitive flag works.