How to search a word separately through regex?
Suppose we have some lines below from where we just want to cut out ‘hello’ regardless of its case.
Lines:
fetch hello only
not hello-world
available hello
not the worldhello
but Hello ok
Regex will be:
(?i)\bhello(?=\s|$)
Output:
fetch hello only
not hello-world
available hello
not the worldhello
but Hello ok
Explanation of regex:
(?i): Case-insensitive flag.
\b: Represents ‘hello’ as a whole word. (\b is word boundary).
hello: Represents the actual word to match.
(?=: Represents the starting of positive lookahead.
\s: Matches any whitespace character like space, tab, newline etc.
|: Represents an ‘OR’ operator.
$: Matches the end of the string.
): Represents ending of the positive lookahead.