'Python re.sub function
I need to convert a lot of view queries to BigQuery syntax. My python script has been working great at replacing pieces of code with re.sub. I need to remove a line containing "LOCKING ROW FOR ACCESS" What is wrong with this:
file_content = re.sub(r'LOCKING ROW FOR ACCESS', '', file_content)
Solution 1:[1]
You're not removing the whole line, you're just removing that part of the line.
file_contents = re.sub(r'.*LOCKING ROW FOR ACCESS.*', '', file_content)
.*
will match anything around the string up to, but not including, the newlines.
Solution 2:[2]
re.sub
will only remove the part of file_content
that is matching with the regex. e.g.,
>>> import re
>>> file_content = 'asdfas LOCKING ROW FOR ACCESSasdfasdf'
>>> file_content = re.sub(r'LOCKING ROW FOR ACCESS', '', file_content)
>>> file_content
'asdfas asdfasdf'
OTOH,
>>> file_content = 'asdfas LOCKING ROW FOR ACCESSasdfasdf'
>>> file_content = re.sub(r'.*LOCKING ROW FOR ACCESS.*', '', file_content)
>>> file_content
''
This is because adding .*
in the beginning will match everything from beginning of the line, similarly for .*
at the end of the string.
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 | Barmar |
Solution 2 | A. K. |