簡體   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