使用 Java 模式的正则表达式匹配字符串中的四个重复字母

发布于 2024-08-28 17:37:36 字数 76 浏览 4 评论 0原文

我想匹配 aaaa、aaaad、adjjjjk 等内容。像 ([az])\1+ 这样的东西被用来匹配重复的字符,但我无法弄清楚这四个字母。

I want to match something like aaaa, aaaad, adjjjjk. Something like ([a-z])\1+ was used to match the repeated characters, but I am not able to figure this out for four letters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丢了幸福的猪 2024-09-04 17:37:36

您想要匹配单个字符,然后该字符再重复 3 次:

([a-z])\1{3}

注意:在 Java 中,您需要转义正则表达式中的反斜杠。


更新:它没有执行您想要的操作的原因是因为您正在使用方法 matches 要求字符串与正则表达式完全匹配,而不仅仅是这样它包含正则表达式。要检查包含情况,您应该使用 匹配器类。这是一些示例代码:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

class Program
{
    public static void main(String[] args)
    {
        Pattern pattern = Pattern.compile("([a-z])\\1{3}");
        Matcher matcher = pattern.matcher("asdffffffasdf");
        System.out.println(matcher.find());
    }
}

结果:

true

You want to match a single character and then that character repeated three more times:

([a-z])\1{3}

Note: In Java you need to escape the backslashes inside your regular expressions.


Update: The reason why it isn't doing what you want is because you are using the method matches which requires that the string exactly matches the regular expression, not just that it contains the regular expression. To check for containment you should instead use the Matcher class. Here is some example code:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

class Program
{
    public static void main(String[] args)
    {
        Pattern pattern = Pattern.compile("([a-z])\\1{3}");
        Matcher matcher = pattern.matcher("asdffffffasdf");
        System.out.println(matcher.find());
    }
}

Result:

true
夏の忆 2024-09-04 17:37:36

不了解有限重复语法,您自己的问题解决能力应该会引导您这样做:

([a-z])\1\1\1

显然这并不漂亮,但是:

  • 它有效
  • 它锻炼了您自己的问题解决能力
  • 它可能会让您更深入地理解概念
    • 在本例中,了解有限重复语法的脱糖形式

我有一个问题:

  • "ffffffff".matches("([az])\\1{3,}") = true
  • "fffffasdf".matches("([az])\\1{3,}") = false
  • "asdffffffasdf".matches("([az])\\1{3,}") = false

我能为底部的两个做什么?

问题是在Java中,matches需要匹配整个字符串;就好像该模式被 ^$ 包围。

不幸的是,没有 String.containsPattern(String regex),但您始终可以使用这个用 .* 包围模式的技巧:

"asdfffffffffasf".matches(".*([a-z])\\1{3,}.*") // true!
//                         ^^              ^^

Not knowing about the finite repetition syntax, your own problem solving skill should lead you to this:

([a-z])\1\1\1

Obviously it's not pretty, but:

  • It works
  • It exercises your own problem solving skill
  • It may lead you to deeper understanding of concepts
    • In this case, knowing the desugared form of the finite repetition syntax

I have a concern:

  • "ffffffff".matches("([a-z])\\1{3,}") = true
  • "fffffasdf".matches("([a-z])\\1{3,}") = false
  • "asdffffffasdf".matches("([a-z])\\1{3,}") = false

What can I do for the bottom two?

The problem is that in Java, matches need to match the whole string; it is as if the pattern is surrounded by ^ and $.

Unfortunately there is no String.containsPattern(String regex), but you can always use this trick of surrounding the pattern with .*:

"asdfffffffffasf".matches(".*([a-z])\\1{3,}.*") // true!
//                         ^^              ^^
木落 2024-09-04 17:37:36

您可以将 {n} 放在某个内容后面以匹配它 n 次,因此:

([a-z])\1{3}

You can put {n} after something to match it n times, so:

([a-z])\1{3}
记忆里有你的影子 2024-09-04 17:37:36

预定义重复的一般正则表达式模式是 {4}

因此这里 ([az])\1{3} 应匹配您的 4 个字符。

General regex pattern for predefinite repetition is {4}.

Thus here ([a-z])\1{3} should match your 4 chars.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文