Complex Regex that can filter word

Can someone help me with correct regex to match line1 and not line2. I want the match criteria to be based on

  • if the line starts with 'username' followed by 1 or more spaces
  • followed by a number and one or more spaces
  • followed by any string not containing 'grep' and ending with 'some/path/somescript.py'
  • The example below should, match line1 and not line2

    line1 = "username   842 93.0  0.1 180740 36016 ?        Sl   Jan21 747:36 /some/path/somescript.py"
    line2 = "username  8227  0.0  0.0  14356  2496 pts/5    S+   11:33   0:00 grep /some/path/somescript.py"
    

    I tried (^usernames*)(d+s*) which meets Nos. 1 & 2 but am not sure how to meet the 3rd requirement. This online tool might be helpful for test


    Try this pattern:

    ^usernames+d+s+(?!.*grep).*some/path/somescript.py$
    

    Demo

    There is not much to explain here, except for this:

    (?!.*grep)
    

    This is a negative lookahead assertion, which says that at this exact spot in the pattern, to assert that we do not find grep anywhere in the remainder of the string. That assertion having been found true, we then match:

    .*some/path/somescript.py$
    

    That is, we match anything so long as the line ends in some/path/somescript.py .

    链接地址: http://www.djcxy.com/p/13470.html

    上一篇: 如何使用subprocess.Popen通过管道连接多个进程?

    下一篇: 复杂的正则表达式,可以过滤单词