Grepping words for match in a file
Jason Friedman
jsf80238 at gmail.com
Sat Dec 28 11:05:33 EST 2019
>
>
> I have some lines in a text file like
> ADD R1, R2
> ADD3 R4, R5, R6
> ADD.MOV R1, R2, [0x10]
> ....
Actually I want to get 2 matches. ADD R1, R2 and ADD.MOV R1, R2, [0x10]
> because these two lines are actually "ADD" instructions. However, "ADD3" is
> something else.
>
>>> s = """ADD R1, R2
... ADD3 R4, R5, R6
... ADD.MOV R1, R2, [0x10]"""
>>> import re
>>> pattern = re.compile(r"^ADD\b.+")
>>> for line in s.splitlines():
... if re.search(pattern, line):
... print(line)
...
ADD R1, R2
ADD.MOV R1, R2, [0x10]
More information about the Python-list
mailing list