检查某些文本是否仅由某些模式组成
我有以下问题:我试图检查某些文本是否仅包含某些模式的多次重复。即我有一个 1000 行文本,想要检查它是否仅包含
asd
123
我尝试不匹配模式,但 (pattern)+
希望它能匹配任何要匹配的内容,但它没有有用。我的另一个想法是将字符串与正则表达式上的文本分开,但它也不起作用。如果重要的话,我正在使用 python re 模块编写此内容。谢谢!
I have the following problem: I'm trying to check whether some text consists only of a number of repetitions of some pattern. I.e I have a 1000 lines text and want to check if it consists only of
asd
123
I tried matching not the pattern, but (pattern)+
hoping that it will match whatever there is to match but it was to no avail. My other idea was to split the string with the text upon the regex, but it didn't work either. I'm writing this using python re module if it matters. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将字符串与
^(?:asd|123)+$
进行匹配,如果匹配,则它仅包含asd
或123
的组合(至少一个)。Try matching the string with
^(?:asd|123)+$
, if it matches, then it only contains combinations ofasd
or123
(at least one).也许您的意思是:
^(abc|123)+$
Perhaps you meant:
^(abc|123)+$
我还没有在Python中尝试过这个,但是您所要求的正则表达式如下:
阅读以下网页,看看它是否能让您更好地理解:
尝试 正则表达式.info/reference.html" rel="nofollow">http://www.regular-expressions.info/reference.html
希望这有帮助!
I haven't tried this in Python, but the regex for what you are asking for would be the following:
Try reading through the following webpage and see if it gives you a better understanding:
http://www.regular-expressions.info/reference.html
Hope this helps!