Case insensitive Regex in Python

13,430

Try this:

expr = re.match(r'.? from\s+(.?)(\s.*|$)', q, re.IGNORECASE)
Share:
13,430

Related videos on Youtube

Vishal K Nair
Author by

Vishal K Nair

Updated on June 04, 2022

Comments

  • Vishal K Nair
    Vishal K Nair almost 2 years

    I have following Python regex:

    re =re.match(r'.*? from\s+(.*?)(\s.*|$)', q)
    

    Here, q is a query like this:

    Q1 = u"select * from dlpx_jobs where job_id=\\'531\\';"
    
    Q2 = u"select * FROM dlpx_jobs where job_id=\\'531\\';"
    

    Now, obviously, for Q1 the regex works because "from" is lowercase in the query but for Q2 regex doesn't work because in Q2 "from" is in Uppercase.

    Is there any way through which the regex works for both the query irrespective of whether "from" is uppercase or lowercase?

    • rock321987
      rock321987 over 6 years
      use inline modifier (?i)