Can regex groups and * wildcards work together?

Is there a way to combine groups and * regular expression functions to act like a tokenizer / delimiter. I tried this:

my_str = "foofoofoofoo"
pattern = "(foo)*"
result = re.search(pattern, my_str)

I was hoping my bands would look like

("foo", "foo", "foo", "foo")

But this is not so. I was surprised by this because? and group functions work together:

my_str= "Mr foo"
pattern = "(Mr)? foo"
result = re.search(pattern, my_str)
+5
source share
2 answers

The problem is that you are repeating your only capture group . This means that you have only one bracket ==> one capture group, and this capture group is overwritten every time it matches.

. . ( , )

, , , 1 "foo".

:

my_str = "foofoofoofoo"
pattern = "foo"
result = re.findall(pattern, my_str)

['foo', 'foo', 'foo', 'foo']

+4

* re-use. findall.

pypi regex, , , , .

+3

All Articles