Home > Blockchain >  Find two occurences in the same line
Find two occurences in the same line

Time:11-10

I have the output of a command, something like this:

machine: alias_machine: status Idle - true -
machine2: alias2: status Idle - true    -
machine3: alias3: status Idle - false    -
machine4: alias4: status Charging - False    -

I have to search if, in one of those machines, the status is Idle and False (at the same time).

To do that, I use this:

if output.find("Idle") and output.find("false"):
    print ("Blablah")

But the problem is, that "Idle" and "false" could be found in other lines, so this does not do the trick.

I also tried this, and it works:

if output.find("Idle - false"):
    print ("Blablah")

But there is a problem, if there is an update in the software or something and the output changes (whitespaces, breaklines, tab, etc), I would have to rewrite the code again...

If I transform it into a list, the format is going to be changed... So I could not use the result later...

CodePudding user response:

Use a regular expression to match Idle followed by false on the same line:

import re

if output.search(r'Idle.*false'):
    print("blahblah")

By default . will not match newlines, so this will match both words on the same line, regardless of what's between them.

  • Related