How to match one per line in a regex?

前端 未结 3 576
遇见更好的自我
遇见更好的自我 2021-01-20 11:00

I\'m having a problem with a regex I created. My company searches for errors in an error file and tries to match the file to a set of possible strings. If one of the strings

相关标签:
3条回答
  • 2021-01-20 11:03

    If you check the parameters of re.subn(pattern, repl, string, count=0, flags=0) you see that there is a parameter count, which you can set to 1.

    0 讨论(0)
  • 2021-01-20 11:04

    You can use the parameter count within the re.subn function. This is to indicate the maximum number of replacements to be done.

    number_of_errors = re.subn(
        r"(is a duplicate for this vendor|Duplicate transaction detected)", "", string, count = 1)[1]
    
    0 讨论(0)
  • 2021-01-20 11:25

    Yes, use

    r"(?m)^.*?(is a duplicate for this vendor|Duplicate transaction detected)"
    

    See proof. The (?m)^.*? part makes the pattern match at the start of each line since the caret matches the line start position and the .*? matches any zero or more characters other than linebreaks, but as few as possible.

    0 讨论(0)
提交回复
热议问题