繁体   English   中英

Java正则表达式查找数字模式

[英]Java regex to find pattern of digits

可以说我有一个数字序列

1 2 3 4 1 2 3 4

如您所见,这里有一个循环:

1 2 3 4

现在,我正在尝试制作一个正则表达式来匹配模式。 模式可以是序列长度的一半或2个数字长。 这就是我到目前为止

Pattern cycle = Pattern.compile("((\\d+\\s)+(\\d+\\s))\\1");

我得到了一串用空格分隔的数字。 我正在尝试使用捕获组,但不了解。 有什么帮助吗?

您可以使用此:

(\d(?:\s+\d)+)\s+\1

这将匹配由空格分隔的两个或多个数字,对其进行捕获,然后查找在另一个空格字符之后立即捕获的内容:

(            # begin capturing group
    \d       # a digit, followed by
    (?:      # begin non capturing group
        \s+  # one or more space characters, followed by
        \d   # a digit
    )+       # end non capturing group, repeated once or more,
)            # end capturing group `\1`, followed by
\s+          # one or more spaces, followed by
\1           # the exact content of the first captured group

注意:假设重复中的间隔完全相同

我只是想更好地理解这个问题。 您的问题是否类似于检测给定字符串中的循环/模式?

类似于这种模式吗?

 "([0-9]+?)\1+"

此模式尝试捕获给定数字流中的重复模式。

与这个问题中讨论的类似,我的regex模式在Python中寻找重复周期有什么问题?

暂无
暂无

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

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