REGEX-匹配字符串中存在的字符

时间:2015-01-15 06:20:03

标签: regex string

接受像这样的字符串的REGEX是什么

  • 从EDO开始
  • 中间有许多字符(单词,数字,超级汉字)
  • 不包含24或|(管道)

实施例: 应该匹配

edo-<<characters>>-<<characeters>>-<<numbers>>

但不是

edo-<<characters>>-<<characeters>>-<<numbers>> | <<characeters>>- <<characeters>>- <<numbers>>

字符串没有恒定长度

2 个答案:

答案 0 :(得分:1)

negative look ahead将帮助您确定字符串是否包含24|

正则表达式可以写成

/^edo(?!.*(24|\|))[-a-zA-Z0-9]+$/i

Regex Demo

如何匹配

  • ^将正则表达式锚定在字符串的开头

  • edo锚定确保字符串以edo

  • 开头
  • (?!.*(24|\|))展望未来。它会检查字符串是否包含24|。如果它不包含,则继续使用剩余的模式。如果包含,则丢弃匹配

  • [-a-zA-Z0-9]+匹配数字字母或-

  • $将正则表达式锚定在字符串的末尾。

答案 1 :(得分:0)

^EDO(?!.*(?:(?<!\d)24(?!\d)|\|))[a-zA-Z0-9 -]+$

试试这个。这应该有效。使用标记gmi

参见演示。

https://regex101.com/r/fA6wE2/37

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  EDO                      'EDO'
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                         (matching the most amount possible))
--------------------------------------------------------------------------------
    (?:                      group, but do not capture:
--------------------------------------------------------------------------------
  (?<!                     look behind to see if there is not:
--------------------------------------------------------------------------------
    \d                       digits (0-9)
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  24                       '24'
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    \d                       digits (0-9)
--------------------------------------------------------------------------------
  )                        end of look-ahead
     |                        OR
--------------------------------------------------------------------------------
      \|                       '|'
--------------------------------------------------------------------------------
    )                        end of grouping
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  [a-zA-Z0-9-]+            any character of: 'a' to 'z', 'A' to 'Z',
                       '0' to '9', '-' (1 or more times (matching
                       the most amount possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                       string