Python Forum
Search in a file using regular expressions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Search in a file using regular expressions
#1
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,
Gribouillis write Dec-17-2024, 12:34 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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.
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Thank you, it is clear now!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 956 Dec-19-2024, 11:57 AM
Last Post: snippsat
  Use or raw string on regular expressions Zaya_pool 5 1,600 May-09-2024, 06:10 PM
Last Post: Zaya_pool
Information Do regular expressions still need raw strings? bobmon 3 2,220 May-03-2024, 09:05 AM
Last Post: rishika24
  Search Excel File with a list of values huzzug 4 2,683 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Search for multiple unknown 3 (2) Byte combinations in a file. lastyle 7 3,123 Aug-14-2023, 02:28 AM
Last Post: deanhystad
  Recursive regular expressions in Python risu252 2 4,744 Jul-25-2023, 12:59 PM
Last Post: risu252
Sad Regular Expressions - so close yet so far bigpapa 5 2,084 May-03-2023, 08:18 AM
Last Post: bowlofred
  search file by regex SamLiu 1 1,639 Feb-23-2023, 01:19 PM
Last Post: deanhystad
  Regular Expression search to comment lines of code Gman2233 5 2,698 Sep-08-2022, 06:57 AM
Last Post: ndc85430
  If function is false search next file mattbatt84 2 1,886 Sep-04-2022, 01:56 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020