正则表达式确定字符串是否是单个重复字符

正则表达式确定字符串是否是单个重复字符

本文介绍了正则表达式确定字符串是否是单个重复字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 确定字符串是否仅包含单个重复字符的正则表达式模式是什么?What is the regex pattern to determine if a string solely consists of a single repeating character?例如。 这个问题检查字符串是否只包含重复字符(例如aabb)但是我需要确定它是否是单个重复字符。This question checks if a string only contains repeating characters (e.g. "aabb") however I need to determine if it is a single repeating character.推荐答案你可以试试后退参考^(.)\1{1,}$ DEMO模式说明: ^ the beginning of the string ( group and capture to \1: . any character except \n ) end of \1 \1{1,} what was matched by capture \1 (at least 1 times) $ the end of the string 反向引用与之前匹配的相同文本相匹配捕获组。反向引用 \1 (反斜杠1)引用第一个捕获组。 \1 匹配第一个捕获组匹配的完全相同的文本。Backreferences match the same text as previously matched by a capturing group. The backreference \1 (backslash one) references the first capturing group. \1 matches the exact same text that was matched by the first capturing group.在Java中你可以尝试In Java you can try"aaaaaaaa".matches("(.)\\1+") // true不需要 ^ 和 $ 因为 String.matches()查找整个字符串匹配。There is no need for ^ and $ because String.matches() looks for whole string match. 这篇关于正则表达式确定字符串是否是单个重复字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-01 20:01