简体   繁体   English

正则表达式-如果结尾字符为“-”,则不匹配

[英]Regex - No match if trailing character is “-”

I have a regex as below 我有一个正则表达式如下

^Schedule\s?(A|B|C|D|E|F|H|J|K|L|M|R|SE)?

so this will match anything like " Schedule A i need help". 因此将与“ 计划表A我需要帮助”之类的内容匹配。

I want restriction for the character "-" ie it should not give a match if the string is like "Schedule A - i need help" . 我想要限制字符“-”,即,如果字符串像“ Schedule A-我需要帮助”,则不应给出匹配项。

But it should give a match if Schedule A is followed by anything other than a space and "-". 但是,如果Schedule A后面跟有空格和“-”,则应给出匹配项。

Negative look aheads will be helpful here 负面的展望将对您有所帮助

^Schedule\s*([ABCDEFHJKLMR]|SE)(?!\s+-)
  • (?!\\s+-) Negative look ahead, checks if the matches string is not followed by space ( \\s+ ) and the - . (?!\\s+-)否定向前看,检查matchs字符串是否后跟空格( \\s+ )和-

  • Note The optional quantifiers ? 注意可选的量词? are not required as it causes the regex engine to skip them. 不需要,因为它会导致正则表达式引擎跳过它们。

  • [ABCDEFHJKLMR] Character class, matches a single character from this set . [ABCDEFHJKLMR]字符类,匹配该集合中的单个字符。

Regex Demo 正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM