简体   繁体   English

正则表达式不匹配序列中的两个相同字符

[英]Regular expression to not to match two same character in the sequence

I know this is very simple for you guys, but i'm scratching my head to find the regular expression to match below strings. 我知道这对你们来说非常简单,但是我正在努力寻找正则表达式来匹配字符串以下的内容。 Have tried different reg ex, but failed every time. 尝试过不同的reg ex,但每次都失败。 Below are the string that needs to be matched/unmatched. 以下是需要匹配/不匹配的字符串。

S__ATHEESH – not match
S_A_T_HEESH – match 
S’_ATHEESH – match 
S’’ATHEESH – not match
S-A_THEESH - match

and here is the requirement 这是要求

a.       A name can have the special characters – space, apostrophe, underscore and hyphen. 
b.      There can be more than 1 special character in a name, but same special character cannot repeat more than once continuously. 
c.       There should be minimum 2 characters entered in the name field 

I have below reg ex, that needs to be modified 我在reg ex以下,需要修改

^([a-zA-Z]+[ _'-])*[a-zA-Z]+$

Thanks for your help. 谢谢你的帮助。

You can use a lookahead based solution like 您可以使用基于前瞻性的解决方案,例如

/^(?!.*([-_ ’])\1)(?=(?:[^a-z]*[a-z]){2})[-_ ’a-z]+$/i
  • (?!.*([-_ '])\\1) checks, that there is no doubled special character (?!.*([-_ '])\\1)检查是否没有双倍的特殊字符
  • (?=(?:[^az]*[az]){2}) checks the presence of at least two letters (?=(?:[^az]*[az]){2})检查是否存在至少两个字母
  • ^[-_ 'az]+$ matches, if only the allowed characters are present 如果仅存在允许的字符,则^[-_ 'az]+$匹配

I used case-insenstive modifier to avoid writing a-zA-Z all the time. 我使用不区分大小写的修饰符来避免一直写a-zA-Z I'm not sure ' is the correct hyphen character, but it's the one you used. 我不确定'是否是正确的连字符,但这是您使用的连字符。

Here's a demo: https://regex101.com/r/fD1gV0/1 这是一个演示: https : //regex101.com/r/fD1gV0/1

The following regular expression will meet your requirements: 以下正则表达式将满足您的要求:

^(?=.{2})(?:[a-zA-Z]+|([ _’'-])(?!\1))+$
  • ^ assert position at the beginning of the string ^在字符串开头的断言位置
  • (?=.{2}) at least 2 characters (?=.{2})至少2个字符
  • and (: 和(:
    • [a-zA-Z]+ at least one alphabetic character [a-zA-Z]+至少一个字母字符
  • or: 要么:
    • ([ _''-]) a space, underscore, magic apostrophe, apostrophe or dash, captured in capture group 1 ([ _''-])在捕获组1中捕获的空格,下划线,魔术撇号,撇号或破折号
    • (?!\\1) negative lookahead that what was just captured isn't also the next character (?!\\1)否定性地回顾刚刚捕获的内容也不是下一个字符
  • ) + as many times as possible +尽可能多
  • $ assert position at the end of the string $在字符串末尾的断言位置

Demo 演示版

Correct me if I'm wrong, but your requirement is simply for strings which do not contains 2 spaces (or apostrophes, underscores or hyphens) one after the other. 如果我错了,请指正我,但是您的要求只是针对不包含两个空格(或撇号,下划线或连字符)的字符串。

In RegEx land, AFAICT, it's not straightforward to assert a "not", but the requirement seems simple enough to be treated in reverse: 在RegEx领域(AFAICT)中,断言“不”并非易事,但要求似乎很简单,可以反过来处理:

(  |__|''|’’)

will match what you mark as "not match", so you just have to negate the boolean match result. 将匹配您标记为“不匹配”的内容,因此您只需要取消布尔匹配结果即可。

Good luck. 祝好运。

Edit: just to make sure I'm not misunderstood. 编辑:只是为了确保我不会被误解。 I'm not saying you cannot do exactly what the op is asking, I'm just saying that negating the requirement is a lot simpler and easier to maintain by someone else. 我并不是说您不能完全按照op的要求进行操作,而是要否定该要求会变得更容易,也更容易由其他人维护。

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

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