简体   繁体   English

正则表达式检查集合的第一个和最后一个字符是否不同

[英]Regex expression to check if first and last char of set are different

Say I have a string of only a 's and b 's, like so:假设我有一串只有ab的字符串,如下所示:

aaabbbbababab

How can I construct a regex, that will match the given string only if the second and last character are different?如何构造一个正则表达式,仅当第二个和最后一个字符不同时才匹配给定的字符串?

This is my current attempt, that does the exact opposite (eg matches if second and last character are the same):这是我目前的尝试,完全相反(例如,如果第二个和最后一个字符相同则匹配):

^[ab]([ab])[ab]*\1$

I am using the ECMAScript implementation of regex.我正在使用正则表达式的 ECMAScript 实现。

You can use a capture group for the second char, and only match the last char if it is not the same as capture group 1.您可以将捕获组用于第二个字符,并且仅在与捕获组 1 不同时匹配最后一个字符。

^[ab]([ab])[ab]*(?!\1)[ab]$
  • ^ Start of string ^字符串开头
  • [ab] Match a or b (Note that you can omit | as it means a pipe char in the character class [ab]匹配 a 或 b(请注意,您可以省略|因为它表示字符 class 中的 pipe 字符
  • ([ab]) Capture group 1 , match either a or b ([ab])捕获组 1 ,匹配 a 或 b
  • [ab]* Optionally match a or b [ab]*可选匹配 a 或 b
  • (?!\1) Negative lookahead, assert not the same value as captured in group 1 using the backreference \1 (?!\1)负前瞻,断言与使用反向引用\1在组 1 中捕获的值不同
  • [ab]$ match either a or b at the end of the string [ab]$匹配字符串末尾的 a 或 b

Regex demo正则表达式演示

Another option is immediately do the assertion after the capture group另一种选择是在捕获组之后立即进行断言

^[ab]([ab])(?![ab]*\1$)[ab]*$

Regex demo正则表达式演示

Or if supported, as negative lookbehind might also work.或者,如果支持,因为负面的后视也可能起作用。 This page shows the compatibility for Javascript and lookbehinds此页面显示 Javascript 的兼容性和后视

^[ab]([ab])[ab]*[ab]$(?<!\1)

Regex demo正则表达式演示

If you don't have to validate that the entire string is a bunch of ab s then I would go for something universal like:如果您不必验证整个字符串是一堆ab ,那么我会 go 用于通用的东西,例如:

^.(.).*(?!\1).$
  • ^. - start with one char - 从一个字符开始
  • (.) - put the second char in a capture group (.) - 将第二个字符放入捕获组
  • .* - optionally capture everything moving forward .* - 可选地捕获前进的所有内容
  • (?.\1).$ - ensure the final char is not the same as the second (?.\1).$ - 确保最终字符与第二个字符不同

https://regex101.com/r/k47PUu/1/ https://regex101.com/r/k47PUu/1/

Just for alternatives sake you could try:只是为了替代,你可以尝试:

^[ab]{3,}(?<!^.\1.*(.))$

See an online demo .查看在线演示

  • ^ - Start line anchor. ^ - 起始线锚。
  • [ab]{3,} - 3+ Times literally "a" or "b". [ab]{3,} - 3 次以上的字面意思是“a”或“b”。
  • (?<.^.\1.*(.) - Negative lookbehind to assert that last character is not the same as the 2nd. (?<.^.\1.*(.) - 否定后向断言最后一个字符与第二个字符不同。
  • $ - End line anchor. $ - 结束线锚。

Or, you could try a negative lookahead (which strangely is the most efficient while testing):或者,您可以尝试负前瞻(奇怪的是,这是测试时最有效的):

^(?!.(.).*\1$)[ab]{3,}$

See an online demo .查看在线演示

  • ^ - Start line anchor. ^ - 起始线锚。
  • (?..(.).*\1$) - Negative lookahead to assert 2nd character is not the same as the very last one before the end line anchor. (?..(.).*\1$) - 断言第二个字符的负前瞻与结束行锚点之前的最后一个字符不同。
  • [ab]{3,} - 3+ Times literally "a" or "b". [ab]{3,} - 3 次以上的字面意思是“a”或“b”。
  • $ - End line anchor. $ - 结束线锚。

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

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