ARTICLE AD BOX
Your expression uses a capturing group. When your code is executed, (Hello)? is a capturing group. The first match is "World", where (Hello)? matches empty, thus returning an empty string; the second match is "HelloWorld", where (Hello)? matches "Hello", thus returning "Hello".
If you want the complete output, you should change it to a non-capturing group, so that the function can return the full match.
import re print(re.findall(r"(?:Hello)?World", "World HelloWorld"))The pattern r"(Hello|Hi)?World" can match three strings: "World", "HelloWorld", and "HiWorld", returning "", "Hello", and "Hi" respectively.
But if you test the pattern "(Hello)?World" and the string "World HelloWorld" in regex101.com, it can get the correct values: ['World', 'HelloWorld']. I think this is a BUG in python's re.
According to my search results, regex101 displays the full match results by default, while Python's re.findall() returns the capture group results when they exist in the pattern. This is simply a case of "different default behaviors of the tools."
