Shuai Zhang
Shuai Zhang

Reputation: 2061

How to match sequential string in text using regular expression?

I am using python's re module to match sequential string in text, for example: s = 'habcabcabcj', I try the following code:

import re
re.findall(r'(abc)+', s)

And the result is: ["abc"]

If I want the match result to be ["abcabcabc"], how can I do this?

Upvotes: 1

Views: 125

Answers (1)

user2555451
user2555451

Reputation:

Use a non-capturing group (?:...):

>>> import re
>>> s = 'habcabcabcj'
>>> re.findall(r'(?:abc)+', s)
['abcabcabc']
>>>

Upvotes: 4

Related Questions