-1

I want to match the word "regex" in a given piece of text irrespective of capital letters.

'I like regex a lot.' should match.

'I like REGEX a lot.' should match.

'I like Regex a lot.' should match.

'I like ReGeX a lot.' should match.

'I like RegeX a lot.' should match.

'I like regexa lot' should not match.

'I like regex22 lot' should not match.

Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
Yashu Seth
  • 935
  • 4
  • 9
  • 24
  • Use [`word boundaries`](https://www.regular-expressions.info/wordboundaries.html) and [`case insensitive flag`](https://docs.python.org/3/library/re.html#re.IGNORECASE), see this [`regex`](https://regex101.com/r/Vrydck/2) – Kunal Mukherjee Jun 14 '19 at 07:27
  • I don't see how it is a duplicate to the question tagged. That question talks about reading word boundaries. It does not mention case sensitiveness anywhere in the question. – Yashu Seth Jun 18 '19 at 09:27

1 Answers1

1

If you want to match the word "regex" (case-insensitive) in a string, you can use this regex (in python you can use the re.IGNORECASE flag to make it case-insensitive):

.*\bregex\b.*

Example:

>>> re.match(r'.*\bregex\b.*', 'I like RegeX a lot', re.IGNORECASE)
<_sre.SRE_Match object; span=(0, 18), match='I like RegeX a lot'>
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55