简体   繁体   English

RegEx - 避免匹配以特定字符结尾的单词

[英]RegEx - avoid matching a word ending with a specific character

I am trying to create a regex (regexp) that will avoid matching words ending with '@', '-', ',': ':' and '>'我正在尝试创建一个正则表达式 (regexp),它将避免匹配以 '@'、'-'、',': ':' 和 '>' 结尾的单词

The rules are as follows - the name should begin with @ can have any character after it except the ones above.规则如下 - 名称应以 @ 开头,后面可以有除上述字符之外的任何字符。 So in the following strings: 'zhsvfghzfajhuib@Bobbie?skvshvfhj?G,' - @Bobbie, will match '768huehfvwkjv@Lana97958749ndgjhb.G!'所以在以下字符串中:'zhsvfghzfajhuib@Bobbie?skvshvfhj?G,' - @Bobbie,将匹配'768huehfvwkjv@Lana97958749ndgjhb.G!' - @Lana9 will match ',vbfnhytjnh@Sammie-sjvjhsvfjj!G!kjdbdjb' - @Sammie- will NOT match, because the character after the name is in the above range. - @Lana9 将匹配 ',vbfnhytjnh@Sammie-sjvjhsvfjj!G!kjdbdjb' - @Sammie- 将不匹配,因为名称后面的字符在上述范围内。

My latest attempt is: @(?[A-Za-z]+)[^@-:.>] but all it did was to remove the last character and still matched.我最近的尝试是:@(?[A-Za-z]+)[^@-:.>] 但它所做的只是删除最后一个字符并且仍然匹配。

I tried:我试过了:

  • adding another character in the search @(?[A-Za-z]+).[^@-:.>] but the search just moved to the next character.在搜索中添加另一个字符 @(?[A-Za-z]+).[^@-:.>] 但搜索只是移动到下一个字符。
  • adding a word boundary @(?[A-Za-z]+)\b[^@-::>] which help in some cases but not all添加单词边界 @(?[A-Za-z]+)\b[^@-::>] 这在某些情况下有帮助但不是全部

If supported, you can use an atomic group :如果支持,您可以使用原子组

@(?>(?<name>[A-Za-z]+))[^@\-!:>]

Regex demo正则表达式演示

Another option is using a positive lookahead with a capture group and a backreference as there is no backtracking in lookarounds :另一种选择是使用带有捕获组和反向引用的正向前瞻,因为环视中没有回溯

@(?=([A-Za-z]+))\1[^@\-!:>]

Regex demo正则表达式演示

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

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