0

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

shaik moeed
  • 4,321
  • 1
  • 14
  • 46
  • Possible duplicate of [Does Python have a string 'contains' substring method?](https://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method) – Emily Nov 13 '19 at 07:04

4 Answers4

1

use 'in' keyword

if 'your_str' in line:
   print('Yeah')

Example:

>>> s = 'this has D++ string'
>>> if 'D++' in s:
...    print("OK")
...
OK
>>>
drd
  • 565
  • 2
  • 8
0

Why not with in?

if 'D++' in Line:
    print ("Success") 
shaik moeed
  • 4,321
  • 1
  • 14
  • 46
0

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

oppressionslayer
  • 6,570
  • 2
  • 5
  • 21
0

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
Mykola Zotko
  • 12,250
  • 2
  • 39
  • 53