Home > database >  Python3: re.match a command from a list of comands on a single line seperated by a ;
Python3: re.match a command from a list of comands on a single line seperated by a ;

Time:10-09

I am trying to parse a command line, with 0 or more commands before of after a command.

The <name> is part of the line as well.

Example:

cmd = 'lsg <name>; cd <name>;find . -type f -exec grep -i <name> {} \; -print;lsg ; ps axwwl ' 
pattern = r'.*(find.*-exec.*\\;?.*?;?)(;*.*$)' 
match = re.match(pattern, cmd)

What I am getting is:

find . -type f -exec grep -i <name> {} \;

What I am trying to do is just match the find command, i.e.:

find . -type f -exec grep -i <name> {} \; -print

Any help would be greatly appreciated.

CodePudding user response:

You can use

match = re.search(r'\bfind\s.*-exec\s.*\\;?[^;]*', cmd)
if match:
    print(match.group())

See the regex demo. Details:

  • \bfind - a find word that has no letter/digit/_ right before it, and then
  • \s - a whitespace
  • .* - zero or more chars other than line break chars, as many as possible
  • -exec - an -exec string
  • \s.* - a whitespace and then zero or more chars other than line break chars, as many as possible
  • \\ - a \ char
  • ;? - an optional ; char
  • [^;]* - zero or more chars other than ;.

See a Python demo:

import re
rx = r"\bfind\s.*-exec\s.*\\;?[^;]*"
text = r"lsg <name>; cd <name>;find . -type f -exec grep -i <name> {} \; -print;lsg ; ps axwwl "
match = re.search(rx, text)
if match:
    print (match.group())

# => find . -type f -exec grep -i <name> {} \; -print
  • Related