簡體   English   中英

我的正則表達式模式在Python中找到重復循環有什么問題?

[英]What is wrong with my regex Pattern to find recurring cycles in Python?

我想匹配任何循環周期的字符串。 就像這個數據:

3333333333333333333333333333333333333333 / 1 digit cycle(3)
1666666666666666666666666666666666666666 / 1 digit cycle(6)
1428571428571428571428571428571428571428 / 6 digit cycle(142857)
1111111111111111111111111111111111111111 / 1 digit cycle(1)
0909090909090909090909090909090909090909 / 2 digit cycle(09)
0834522467546323545411673445234655345222 / no cycle
0769230769230769230769230769230769230769 / 6 digit cycle(769230)
0714285714285714285714285714285714285714 / 6 digit cycle(714285)
0666666666666666666666666666666666666666 / 1 digit cycle(6)

我試過的模式是"([0-9]+?)\\1+" ,它在其他語言(如VB或文本編輯器)中運行良好。 我已將這些字符串存儲在名為values的列表中。 所以這是我的代碼:

import re

#stuff to get values
pattern = re.compile("([0-9]+?)\1+")
for value in values:
    matchObj = pattern.search(value)
    print(matchObj) #-> None
    matchObj = pattern.findall(value)
    print(matchObj) #-> []

我究竟做錯了什么? 任何提示都表示贊賞。

添加r前綴:

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

這將使反斜杠成為字面反斜杠而不是逃避1。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM