Search code examples
pythonregexpython-re

How to capture between two characters in python using regex?


I am trying to get everything between /* and */

I am using re library and following expression to get data

pattern = re.compile(r'(?<=\/\*)(.|\n)+?(?=\*\/)')
result = pattern.findall(query)

query - a string with data shown in the image (may contain line breaks, etc.)

my expression is not working correctly and I get this result

['\n', '\n', '\n', '\n']

how can I get all content in between /* and */?

enter image description here


Solution

    1. Use re.DOTALL to make . match newlines
    2. Don't escape forward slashes
    3. Non-capturing syntax is: (?:<expr>)
    >>> txt = """
    /*
    name = foo
    table = bar
    */ don't capture this
    """
    >>> re.findall(r'(?:/\*)(.*?)(?:\*/)', txt, re.DOTALL)
    ['\nname: foo\ntable: bar\n']