Reputation: 41
I need to search for a particular word in a log file For example, 255 1237 92 D++
I want to scan through each line whether the word "D++" is present. I have tried the approach below but failed.
if re.search(r"D\+\+", Line) is not None:
print ("Success")
Thanks
Upvotes: 0
Views: 167
Reputation: 17794
I don't see any mistake in your solution. You can simplify your code:
if re.search(r'D\+{2}', '255 1237 92 D++'):
print('Success')
# Success
Upvotes: 0
Reputation: 605
use 'in' keyword
if 'your_str' in line:
print('Yeah')
Example:
>>> s = 'this has D++ string'
>>> if 'D++' in s:
... print("OK")
...
OK
>>>
Upvotes: 1
Reputation: 7204
You can use strings .find
'255 1237 92 D++'.find('D++') > -1
Out[329]: True
In [330]: '255 1237 92 D++'.find('sdf') > -1
Out[330]: False
Upvotes: 0