0

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 !??

  • 1
    Does this answer your question? [How do I match any character across multiple lines in a regular expression?](https://stackoverflow.com/questions/159118/how-do-i-match-any-character-across-multiple-lines-in-a-regular-expression) (Note: this question is a canonical for multiple languages, Ctrl-F "python" for the python-specific solution) – SuperStormer Mar 02 '22 at 00:05

1 Answers1

0

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.

gajendragarg
  • 471
  • 1
  • 4
  • 11