Python Forum

Full Version: Search in a file using regular expressions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all,

I have the following file with DB_BASE commented:

profile.txt

export DB_SID=NEW
#export DB_BASE=/oracle/${DB_SID}
export DB_HOME=$DB_BASE/19
export PATH=$PATH:$DB_HOME/bin
export NLS_LANG=AMERICAN_AMERICA.UTF8

I need to search in the file and using regular expressions to check if DB_BASE is commented or not. Below is my match:
match = re.search('r(.*)#.*DB_BASE|.*DB_BASE',line)
and the code:
import re
with open('profile.txt', 'r') as profile_file:
        file_content = profile_file.readlines()
        #print(file_content)
        file_content_new = list(map(lambda x:x.strip(),file_content))
        #print(file_content_new)
        for line in file_content_new:
                match = re.search('r(.*)#.*DB_BASE|.*DB_BASE',line)
                print(match)
                if match:
                        if match.group(1) is not None and re.search(r'#', match.group(1)):
                                print('DB_BASE is commented')
                else:
                        print('DB_BASE is not commented')
It doesn't matter what I am doing, i receive always that the DB is not commented....
Output:
['export DB_SID=NEW\n', '#export DB_BASE=/oracle/${DB_SID}\n', 'export DB_HOME=$DB_BASE/19\n', 'export PATH=$PATH:$DB_HOME/bin\n', 'export NLS_LANG=AMERICAN_AMERICA.UTF8\n'] ['export DB_SID=NEW', '#export DB_BASE=/oracle/${DB_SID}', 'export DB_HOME=$DB_BASE/19', 'export PATH=$PATH:$DB_HOME/bin', 'export NLS_LANG=AMERICAN_AMERICA.UTF8'] None DB_BASE is not commented <_sre.SRE_Match object at 0x7f5479ccf230> <_sre.SRE_Match object at 0x7f5479cd95b0> None DB_BASE is not commented None DB_BASE is not commented
The logic is not correct, or am I doing something wrong....Could you please help me?
Thank you,
The r should be out of the string, if you want to write a raw string, so r'....' instead of 'r....'

You can also try this code
import io
import re

file = io.StringIO(
    """\
export DB_SID=NEW
#export DB_BASE=/oracle/${DB_SID}
export DB_HOME=$DB_BASE/19
export PATH=$PATH:$DB_HOME/bin
export NLS_LANG=AMERICAN_AMERICA.UTF8
"""
)

for line in file:
    match = re.match(r"[^#]*?(#?).*?DB_BASE\W", line)
    if match:
        if match.group(1):
            print("DB_BASE is commented")
        else:
            print("DB_BASE is not commented")
    else:
        print("DB_BASE is not present")
Output:
λ python paillasse/pf/adele80.py DB_BASE is not present DB_BASE is commented DB_BASE is not commented DB_BASE is not present DB_BASE is not present
Now the problem is that if you comment out the DB_HOME line, it will now show "DB_BASE is commented" for this line, so we should perhaps replace the regex with r"\s*(#?)\s*export\s+DB_BASE\W" which works better for this case.
Thank you, it is clear now!