选择除外?

时间:2015-04-24 15:41:17

标签: regex

我需要创建一个正则表达式,除了2个单词之外,在字符串后面找到任何内容: 我有:

Transport input telnet
transport input ssh
transport input tftp
transport input lat
transport input pat

我需要的是一个表达式,除了telnet和ssh之外,它将在传输输入后找到tftp,lat和pat或任何东西:
类似的东西:

transport input (.*) except (telnet|ssh)

由于

2 个答案:

答案 0 :(得分:4)

使用negative lookahead assertion

transport input (?!(?:telnet|ssh)$)(.*)

DEMO

答案 1 :(得分:2)

或者,您可以使用negative lookbehind

transport input (.*)(?<!telnet|ssh)$

DEMO

正则表达式说明:

transport input (.*)(?<!telnet|ssh)$


Match the character string “transport input ” literally (case insensitive) «transport input »
Match the regex below and capture its match into backreference number 1 «(.*)»
   Match any single character that is NOT a line break character (line feed) «.*»
      Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Assert that it is impossible to match the regex below with the match ending at this position (negative lookbehind) «(?<!telnet|ssh)»
   Match this alternative (attempting the next alternative only if this one fails) «telnet»
      Match the character string “telnet” literally (case insensitive) «telnet»
   Or match this alternative (the entire group fails if this one fails to match) «ssh»
      Match the character string “ssh” literally (case insensitive) «ssh»
Assert position at the end of a line (at the end of the string or before a line break character) (line feed) «$»