Jun Fai Heu
Jun Fai Heu

Reputation: 41

How to search for specific word in a single line

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

Answers (4)

Mykola Zotko
Mykola Zotko

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

drd
drd

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

oppressionslayer
oppressionslayer

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

shaik moeed
shaik moeed

Reputation: 5785

Why not with in?

if 'D++' in Line:
    print ("Success") 

Upvotes: 0

Related Questions