简体   繁体   English

我的正则表达式模式在Python中找到重复循环有什么问题?

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

I want to match any string that has a recurring cycle. 我想匹配任何循环周期的字符串。 Like in this data: 就像这个数据:

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)

The pattern I have tried is "([0-9]+?)\\1+" which works well in other languages (like VB or text editors). 我试过的模式是"([0-9]+?)\\1+" ,它在其他语言(如VB或文本编辑器)中运行良好。 I have stored these strings inside a list named values . 我已将这些字符串存储在名为values的列表中。 So here is my code: 所以这是我的代码:

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) #-> []

What am I doing wrong? 我究竟做错了什么? Any hint is appreciated. 任何提示都表示赞赏。

Add an r prefix: 添加r前缀:

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

That will make the backslash a literal backslash instead of escaping the 1. 这将使反斜杠成为字面反斜杠而不是逃避1。

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

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