python - Can Regex groups and * wildcards work together? -


Do combine regular expressions of groups and * features to do kindof functions like an token / splitter Any way? I tried this:

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

I It was hoped that my group might not be

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

but not so. Was I surprised by this reason? And group features work together:

  my_str = "Mr. foo" pattern = "(m)? Foo" result = re-search (pattern, my_str)  < 

The problem is that you repeat your only capturing group This means that your Nearly one bracket is ==> A capturing group, and it captures every time the capturing group is overwritten.

For more information, see Regular-expression.info. (But the repetitive group does not have to capture as much as you want)

Then, after your rajux, your capture will be the last "Foo" in Group 1.

This will give you the expected results:

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

The result is then a list ['foo', 'foo', 'foo', 'foo']

Comments