0

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()

3
  • It's not clear how this pattern is supposed to behave. Why does the second example drop the Some from the output? Why is the period removed? Should other kinds of leading characters be removed too? Would merely splitting the string produce output you can use? Commented Mar 10, 2014 at 2:15
  • @user2357112 Typo on my part, i'll edit it Commented Mar 10, 2014 at 2:31
  • @user2357112 I forgot to address your second question about the leading characters and splitting, but I think regex is better for this problem then trying to split the string. As for the leading characters, if I understand it correctly any character should work no matter if it is a digit, letter, decimal, or anything else. Commented Mar 10, 2014 at 2:38

1 Answer 1

1

Something like this?

In [179]: cmd = 'SomeCommand'

In [180]: s = '.SomeCommand Some extra stuff'

In [189]: command, extra = re.search(r'\.(%s)( *.*)'%cmd, s).groups()
     ...: print command, '----', extra.strip()
SomeCommand ---- Some extra stuff

In [190]: s = '.SomeCommand'

In [191]: command, extra = re.search(r'\.(%s)( *.*)'%cmd, s).groups()
     ...: print command, '----', extra.strip()
SomeCommand ---- 

EDIT:

On your update, it seems your command never contains whitespaces, so just use str.split with the maxsplit being 1:

In [212]: s = '.SomeCommand'

In [215]: s.split(' ', 1)
Out[215]: ['.SomeCommand']

In [216]: s = '.SomeCommand Some extra stuff'

In [217]: s.split(' ', 1)
Out[217]: ['.SomeCommand', 'Some extra stuff']

To avoid the unpacking errors (if you insist on unpacking):

In [228]: parts = s.split(' ', 1)

In [229]: command, extra = parts[0], "" if len(parts)==1 else parts[1]
Sign up to request clarification or add additional context in comments.

5 Comments

Almost, but what if there is just ".SomeCommand" ? It gives me a NoneType error, almost worked though.
@user3234209 if you don't know what the command is, how do you recognize it? give us your rules ;) does the command contains spaces? or wrapped with double quotes?
@user3234209 if your command contains no spaces, see my update
I edited the answer to show the working regex. It's just a modified version of yours so you don't have to use %s and then format the string.
@user3234209 well, it's good, just a bit slower than str.split ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.