1

Given the string bcacaca. The regex pattern b?(.a)* should match on the whole string. However, a call to re.findall('b?(.a)*', 'bcacaca') returns ['ca', ''] as a result. It seems to be only returning the result of matching on the individual groups. What's going on here?

My understanding of findall is that it should return all nonoverlapping instances of the regex pattern. In this case it should return ['bcacaca', ''].

4
  • 3
    That's how findall works: "If one or more groups are present in the pattern, return a list of groups". If you want to match the whole string, change the group to non-capturing b?(?:.a)*
    – Nick
    May 19, 2020 at 2:12
  • I believe you are looking for "re.search(...).goroup()" May 19, 2020 at 2:14
  • @Nick So ?: is included at the start of every group that you want to mark as non-capturing? May 19, 2020 at 2:15
  • 1
    @MutatingAlgorithm correct. See stackoverflow.com/questions/22937618/…
    – Nick
    May 19, 2020 at 2:16

1 Answer 1

1

Use '(b(.a)*)' as your regex pattern instead. You need result[0] in the following example.

import re

result = re.findall('(b(.a)*)', 'bcacaca')
result

Output:

[('bcacaca', 'ca')]

A Better Option - Using a Non-capturing Group

As @Nick mentioned, a non-capturing group could be used here as follows. Consider the following scenario. For step-by-step explanation see the next section. Also, I encourage you to use this resource: regex101.com.

## Define text and pattern
text = 'bcacaca dcaca dbcaca'
pattern = 'b?(?:.a)*'

## Evaluate regex
result = re.findall(pattern, text)
# output
# ['bcacaca', '', '', 'caca', '', '', 'bcaca', '']

## Drop empty strings from result
result = list(filter(None, result))
# output
# ['bcacaca', 'caca', 'bcaca']

Explanation for Using a Non-capturing Group

enter image description here

References

  1. Remove empty strings from a list of strings

Not the answer you're looking for? Browse other questions tagged or ask your own question.