Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Make A Dot Match Newline in the re Module in Python

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Comment

  1. Pass re.DOTALL to the option flags to make a dot match newlines as well.

import re
pattern = ".*select\s+.*\s+from\s+(\w+\.?\w+)\s+"
sql = """
    select
       c1, 
       c2,
    from
        abc.table
    where
        1 = 1
    """.strip().lower()
re.search(pattern, sql)
pattern = ".*select\s+.*\s+from\s+(\w+\.?\w+)\s+"
sql = """
    select
       c1, 
       c2
    from
        db.table
    where
        1 = 1
    """.strip().lower()
match = re.search(pattern, sql, re.DOTALL)
print(match.group(0))
print(match.group(1))
select
       c1, 
       c2
    from
        db.table
    
db.table
pattern = ".*select\s+.*\s+from\s+(\w+\.?\w+)\s+"
sql = """
    select
        count(*) as total,
        count(case
                when item_site_id = 15 then 1
                else 0
            end) as total_au
    from
        db.table
    """
match = re.search(pattern, sql, re.DOTALL)
print(match.group(0))
print(match.group(1))

    select
        count(*) as total,
        count(case
                when item_site_id = 15 then 1
                else 0
            end) as total_au
    from
        db.table
    
db.table
"abc".split(", ")
['abc']
(x,) = ["nima"]
x, *_ = ["nima"]
x
'nima'