[英]Regular expression: repeating patterns in the beginning of string
For example, consider the following string: "apple1: apple2: apple3: some random words here apple4:"
例如,考虑以下字符串: "apple1: apple2: apple3: some random words here apple4:"
I want to match only apple1, apple2 and apple3 but not apple4. 我只想匹配apple1,apple2和apple3,而不匹配apple4。 I am having a hard time to figure out how to archive this. 我很难弄清楚如何将其存档。
Any help is appreciated. 任何帮助表示赞赏。
Thanks. 谢谢。
If you are using .net you can match the below pattern and then use the Captures
property of the group to get all the different apples matched along the way. 如果您使用的是.net,则可以匹配以下模式,然后使用该组的Captures
属性来获取所有匹配的苹果。
(?:(apple\d).*?){3}
If you only want to match the first one: 如果您只想匹配第一个:
apple\d
Sweet and simple. 甜美而简单。 Just call match on this once. 只需对此进行一次比赛。
From your comment, it sounds like you want to match the occurrences of apple
followed by a digit throughout the string except an occurrence of apple
followed by a digit at the end of the string. 从您的评论,这听起来像你想匹配的出现apple
,然后在整个字符串的数字,除了的发生apple
后面跟着一个数字字符串的结尾。
>>> import re
>>> text = 'apple1: apple2: apple3: some random words here apple4:'
>>> matches = re.findall(r'(\bapple\d+):(?!$)', text)
['apple1', 'apple2', 'apple3']
So, maybe something like this: 因此,也许是这样的:
^([A-Za-z]+)[^A-Za-z]+(\1[^A-Za-z]+)+
Sorry guys, I did not format my question properly, it wasn't clear. 抱歉,我的问题格式不正确,目前尚不清楚。
I found the solution: 我找到了解决方案:
r'\s*((apple)\d+[ \:\,]*)+'
Thanks for all your help! 感谢你的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.