Assistance with regex python -


i need regex pattern allows me below i'm not quite sure how to.

command, = re.search(someregexpattern, string).groups() # or split list  input: ".somecommand" command, = "somecommand", "" # "" because there nothing follows "somecommand" input: ".somecommand stuff" command, = "somecommand", "some stuff" input: ".somecommand long text after somecommand" command, = "somecommand", "some long text after somecommand"  

note somecommand dynamic not somecommand

is there regex makes possible? command 1 thing , comes after command assigned extra?

update: seems have not clarified enough of regex should i'm updating answer help.

while true:     text = input("input command: ")     command, = re.search(someregexpattern, text).groups() 

example data

# when text .random  command = "random" = ""  # when text .gis test (for google image search.) command = "gis" = "test"  # when text .somecommand rather long text after command = "somecommand" = "some rather long text after it" 

working regex

command, = re.search("\.(\w+)( *.*)", text).groups() # modified zhangxaochen's answer tad , works, don't forget redefine extra.strip()

something this?

in [179]: cmd = 'somecommand'  in [180]: s = '.somecommand stuff'  in [189]: command, = re.search(r'\.(%s)( *.*)'%cmd, s).groups()      ...: print command, '----', extra.strip() somecommand ---- stuff  in [190]: s = '.somecommand'  in [191]: command, = re.search(r'\.(%s)( *.*)'%cmd, s).groups()      ...: print command, '----', extra.strip() somecommand ----  

edit:

on update, seems command never contains whitespaces, use str.split maxsplit being 1:

in [212]: s = '.somecommand'  in [215]: s.split(' ', 1) out[215]: ['.somecommand']  in [216]: s = '.somecommand stuff'  in [217]: s.split(' ', 1) out[217]: ['.somecommand', 'some stuff'] 

to avoid unpacking errors (if insist on unpacking):

in [228]: parts = s.split(' ', 1)  in [229]: command, = parts[0], "" if len(parts)==1 else parts[1] 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -