-1

I have this input: FBBBBFFRRL

The first 7 characters can be F or B and the last 3 are R or L.

I want to split this into 2 strings so here is what I've done:

regex = re.findall('^((F|B){7})', 'FBBBBFFRRL')
print(regex)

I don't understand why I got this:

[('FBBBBFF', 'F')]

The first item is correct but why do I also get a single F?

1 Answers1

0

The result from findall corresponds to the parentheses in your regular expression. The longer result string corresponds to the first (outer) parentheses, and the second, to whatever matched the inner parentheses in the last iteration.

If you don't want that, use non-capturing parentheses (?:F|B) - or in the case where you just match one out of a set of single characters, a character class [FB].

You can exploit this to check your conditions and partition the string in one go;

matches = re.findall(r'^([BF]{7})([LR]{3})$', your_string)
tripleee
  • 175,061
  • 34
  • 275
  • 318