So you technically can do this with regex, but it isn't advised since you're just checking to see if two characters are equal to something.
If you want to use regex:
pattern = r"^\.B.*\);"
regex = re.compile(pattern)
for filename in os.listdir(path1):
f = gzip.open(path1 + filename, "rb")
for line in f:
if regex.match(line):
file = open("db.txt", "w")
file.write(line)
You don't need to actually have two different regular expressions, you can just see if you start with .B
, followed by whatever and then ending with );
.
The other thing to do is just avoid regular expressions all together if you're not comfortable with them and do something like this instead
for filename in os.listdir(path1):
f = gzip.open(path1 + filename, "rb")
for line in f:
if line[:2] == ".B" and line[-2:] == ");"
file = open("db.txt", "w")
file.write(line)
This creates a string slice to compare against directly. It basically says line[:2]
take all the characters in line up to, but not including the 2nd index and see if that is equal to ".B". Then line[-2:]
take the last two characters of line and compare them to see if they're equal to ");"