General approach for (equivalent of) “backreferences within character class”?

狂风中的少年 提交于 2019-11-30 17:38:59

This can be accomplished with a negative lookahead within a repeated group:

/\A         # match beginning of string;
 (.)        # match and capture first character (referred to subsequently by \1);
 ((?!\1).)* # match zero or more characters different from character in \1;
 \1         # match \1;
 \z         # match the end of the string;
/sx

This pattern can be used even if the group contains more than one character.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!