python – Regular expression problems

Question:

The regular expression finds the wrong thing. Here is the code:

reg_exp = re.compile(r"(A+)|(B+)")
print(reg_exp.findall("AAABBAA"))

Should output: ['AAA', 'BB', 'AA'] Output: [('AAA', ''), ('', 'BB'), ('AA', '')] Where did these tuples with empty strings? How do I rewrite the regex to return what I want?

Answer:

How many brackets do you have? Two pairs? Here is python and outputs. The answer is absolutely logical and correct. But if you want it differently, then write the expression differently

reg_exp = re.compile(r"(A+|B+)")
print(reg_exp.findall("AAABBAA"))
Scroll to Top
AllEscort