Reputation: 23
Why the same regex statement, get different result between Python and grep?
My input string is like as below:
statement or comment should appear in default case [MISRA 2012 Rule 16.1, required], [MISRA 2012 Rule 16.4, required]
The purpose is to find [MISRA 2012 Rule 16.1, required] and [MISRA 2012 Rule 16.4, required] by regex.
Using the code as below in Python,
onelineStr = "statement or comment should appear in default case [MISRA 2012 Rule 16.1, required], [MISRA 2012 Rule 16.4, required]"
r1 = re.findall(r"\[MISRA.*?\]", onelineStr)
print (r1)
the result is shown as below, which is as my expectation.
['[MISRA 2012 Rule 16.1, required]', '[MISRA 2012 Rule 16.4, required]']
However, when I use the same statement of regex in grep. Nothing I can find.
grep -o --color "\[MISRA.*?\]" 1.txt
The content of 1.txt is statement or comment should appear in default case [MISRA 2012 Rule 16.1, required], [MISRA 2012 Rule 16.4, required]
Is anything wrong?
Thank you!!
Upvotes: 1
Views: 176
Reputation: 626952
The .*?
lazy dot pattern matches any 0 or more chars (other than line break chars) as few as possible, but lazy quantifiers are not supported in POSIX regular expressions.
You need to use a [^][]*
in grep
in a POSIX BRE pattern to match zero or more chars other than ]
and [
:
grep -o --color "\[MISRA[^][]*]" 1.txt
Actually, the same pattern can be used in Python re
, too.
[^][]*
is an example of "smart placement" of special chars inside a bracket expression (POSIX bracket expressions do not allow regex escape sequences).
Upvotes: 2