I need help with a regex pattern that allows me to do the below but I'm not quite sure how to.
command, extra = re.search(SomeRegexPattern, string).groups() # or split it to be a list
Input: ".SomeCommand"
command, extra = "SomeCommand", "" # extra is "" because there was nothing that follows "SomeCommand"
Input: ".SomeCommand Some extra stuff"
command, extra = "SomeCommand", "Some extra stuff"
Input: ".SomeCommand Some really long text after SomeCommand"
command, extra = "SomeCommand", "Some really long text after SomeCommand"
Note SomeCommand is dynamic it is not actually SomeCommand
Is there a regex that makes this possible? So that the command is one thing and anything that comes after the command is assigned to extra?
Update: It seems I have not clarified enough of what the regex should do so I'm updating the answer to help.
while True:
text = input("Input command: ")
command, extra = re.search(SomeRegexPattern, text).groups()
Example data
# when text is .random
command = "random"
extra = ""
# when text is .gis test (for google image search.)
command = "gis"
extra = "test"
# when text is .SomeCommand Some rather long text after it
command = "SomeCommand"
extra = "Some rather long text after it"
Working Regex
command, extra = re.search("\.(\w+)( *.*)", text).groups() # modified zhangxaochen's answer just a tad and it works, don't forget to redefine extra as extra.strip()
Somefrom the output? Why is the period removed? Should other kinds of leading characters be removed too? Would merelysplitting the string produce output you can use?