How should I type in Python if
condition to match wildcard expression (*)?
I am trying to find line(s) in file that match wildcard expression - ...string1*string2*string3...
with open(file,'rw') as FILE:
lines = FILE.readlines()
for line in lines:
if line == "string1*string2*string3":
print (line)
CodePudding user response:
Here's a sample:
import re
with open(file,'r') as file:
for line in file.readlines():
if re.search( "string1.*string2.*string3", line ):
print (line)
Of course, if this is ALL you're doing, you don't need Python at all:
grep 'string1.*string2.*string3' file.txt
Or on Windows:
findstr /r "string1.*string2.*string3" file.txt
CodePudding user response:
Here is a solution using regular expressions:
import re
mystring = re.compile('string1.*string2.*string3')
filename = '/path/to/some/file'
with open(filename) as f:
for line in f.readlines():
if mystring.match(line):
print(line)
CodePudding user response:
with open(file,'rw') as FILE:
lines = FILE.readlines()
for line in lines:
if "string1*string2*string3" in line:
print(line)