'is there anyway to regex match everything words, even space and newlines python?
im trying to update some code written in a private language ... anyway the thing is i was trying to use regex in python language to match the rest of the code as in the example :
from re import sub
data = """
Go To --> BB125276 {
Scan:
#f
}
"""
#//////////////////////////////////////
schema = r"""
Go To --> (.+) {
\s*(.+)
}
"""
#//////////////////////////////////////
final_form = r"""
Goto {\1}
\2
"""
#//////////////////////////////////////
massar_code = sub(schema, final_form, data)
print(massar_code)
but it doesn't matches newlines and spaces ... i want it to matches everything any help !??
Solution 1:[1]
You need to add re.DOTALL or re.S flag to the re.sub( ):
massar_code = sub(schema, final_form, data, flags=re.S)
Use of re.S or re.DOTALL:
Make the '.' special character match any character at all, including a newline; without this flag, '.' will match anything except a newline.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | gajendragarg |