正则表达式,以检测字符是否重复了三遍以上 [英] Regex to detect if character is repeated more than three times

查看:393
本文介绍了正则表达式,以检测字符是否重复了三遍以上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试遵循此处描述的解决方案: https://stackoverflow.com/a/17973873/2149915 尝试匹配具有以下要求的字符串: -字符串中顺序重复的3个以上字符应匹配并返回.

I've tried to follow the solution described here: https://stackoverflow.com/a/17973873/2149915 to try and match a string with the following requirements: - More than 3 characters repeated sequentially in the string should be matched and returned.

示例:

  • 你好...->有效
  • 你好,你好.............->无效
  • hiii->有效
  • hiiiiii->无效

依此类推,以此类推,目的是检测无意义的文本.

and so on and so forth, the idea is to detect text that is nonsensical.

到目前为止,我的解决方案是按原样修改链接中的正则表达式.

So far my solution was to modify the regex in the link as such.

原始:^(?!.*([A-Za-z0-9])\1{2})(?=.*[a-z])(?=.*\d)[A-Za-z0-9]+$

已适配:^(?!.*([A-Za-z0-9\.\,\/\|\\])\1{3})$

从本质上讲,我删除了捕获数字和字母数字的组的要求:(?=.*[a-z])(?=.*\d)[A-Za-z0-9]+并尝试添加额外的字符检测功能,例如./,\等,但它似乎根本与任何字符都不匹配...

Essentially i removed the requirement for capture groups of numbers and alphanumerics seen here: (?=.*[a-z])(?=.*\d)[A-Za-z0-9]+ and tried to add extra detection of characters such as ./,\ etc but it doesnt seem to match at all with any characters...

关于我如何实现这一目标的任何想法?

Any ideas on how i can achieve this?

提前谢谢:)

我在这个问题上找到了这个正则表达式:^.*(\S)(?: ?\1){9,}.*$ https://stackoverflow.com/a/44659071/2149915 并有对其进行了修改,使其仅可匹配3个字符,例如^.*(\S)(?: ?\1){3}.*$.

i found this regex: ^.*(\S)(?: ?\1){9,}.*$ on this question https://stackoverflow.com/a/44659071/2149915 and have adapted it to match only for 3 characters like such ^.*(\S)(?: ?\1){3}.*$.

现在它可以检测到以下情况:

Now it detects things like:

  • aaaa->无效
  • 你好.........->无效
  • /////....->无效

但是它不考虑这样的空格:

however it does not take into account whitespace such as this:

  • . . . . .

是否可以进行修改以实现此目的?

is there a modification that can be done to achieve this?

推荐答案

如果您要查找重复超过3次的任何字符,我认为有一个更简单的解决方案:

I think there's a much simpler solution if you're looking for any character repeated more than 3 times:

String[] inputs = {
    "hello how are you...", // -> VALID
    "hello how are you.............", // -> INVALID
    "hiii", // -> VALID
    "hiiiiii" // -> INVALID
};
//                            | group 1 - any character
//                            | | back-reference
//                            | |   | 4+ quantifier including previous instance
//                            | |   |     | dot represents any character, 
//                            | |   |     | including whitespace and line feeds
//                            | |   |     | 
Pattern p = Pattern.compile("(.)\\1{3,}", Pattern.DOTALL);
// iterating test inputs
for (String s: inputs) {
    // matching
    Matcher m = p.matcher(s);
    // 4+ repeated character found
    if (m.find()) {
        System.out.printf(
            "Input '%s' not valid, character '%s' repeated more than 3 times%n", 
            s, 
            m.group(1)
        );
    }
}

输出

Input 'hello how are you............. not valid', character '.' repeated more than 3 times
Input 'hiiiiii' not valid, character 'i' repeated more than 3 times
Input 'hello    how are you' not valid, character ' ' repeated more than 3 times

这篇关于正则表达式,以检测字符是否重复了三遍以上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆