DBS
DBS

Reputation: 1147

Regex to search file for exact string

I have looked at the other questions around this, but I can't quite get those answers to work for my situation.

I have a string that I'm searching for inside a file. I want to match the exact string and if there's a match, do something. I'm trying to formulate regex for the string, "author:". If this string is found, strip it from the line and give me everything to the right of it, removing any whites space. Any ideas looking at the code below in how to achieve this?.

metadata_author = re.compile(r'\bauthor:\b')
with open(tempfile, encoding='latin-1') as search:
    for line in search:
        result = metadata_author.search(line)
        if result in line:
            author = result.strip()
            print(author)

Upvotes: 1

Views: 326

Answers (1)

evsheino
evsheino

Reputation: 2277

I would use a lookbehind (with a negative lookbehind for the possible dot as mentioned in the comment):

metadata_author = re.compile(r'(?<=(?<!\.)\bauthor:).+')
with open(tempfile, encoding='latin-1') as search:
    for line in search:
        result = metadata_author.search(line)
        if result:
            author = result.group().strip()
            print(author)

re.search returns a match object, and not a string, so to get the matching string you have to call result.group().

If you want to remove all whitespace (instead of just trimming), use re.sub(r'\s*', '', result.group()) instead of result.group().strip().

Upvotes: 1

Related Questions