How to match any string with file extension in a row and print only the corresponding word in python

advertisements

I wanted to search and match any word followed by file extension .log.gz in a line and print only the matching word.

Below is the code example I tried,

# Global Import Variables
import Tkinter, Tkconstants, tkFileDialog

line = "helo how are helloo.log.gz you"

line_se = re.search(r"\b.*\.log.gz", line)

if line_se:
    print line_se
    print("yo it's a {}".format(line_se.group(0)))

But the output is the whole line until the extension like yo it's a helo how are helloo.log.gz, but the output I wanted is just helloo.log.gz. Can anyone correct me on the reg exp which I am using to print out the matching word with the extension !

Thanks a ton !


Change .* to \S* so it doesn't match spaces.

ine_se = re.search(r"\S*\.log\.gz", line)