0

I have such a String as an example:

"[greeting] Hello [me] my name is John."

I want to split it and get such a result

('[greetings]', 'Hello' , '[me]', 'my name is John')

Can it be done in one line of code?

OK another example as it seems that many misunderstood the question.

"[greeting] Hello my friends [me] my name is John. [bow] nice to meet you."

then I should get

 ('[greetings]', ' Hello my friends ' , '[me]', ' my name is John. ', '[bow]', ' nice to meet you.')

I basically want to send this kind of string to my robot. It will automatically decompose it and do some motion corresponding to [greetings] [me] and [bow] and in between speak the other strings.

7
  • So you want to split using a space (" ") as a delimiter? Commented Jul 24, 2014 at 5:35
  • Did you try something? Commented Jul 24, 2014 at 5:36
  • Extracting the delimited words, detecting the position of the opening delimiter '[' and using the length of the extracted words ('greetings' and 'me') to finally split the string the way I want it to be split. But I thought Python has more to offer. Commented Jul 24, 2014 at 5:38
  • it's not possible to do in one line. you have use list with phrases with spaces which will evaluate in one phrase after splitting Commented Jul 24, 2014 at 5:47
  • @ZagorulkinDmitry check the accepted answer. Commented Jul 24, 2014 at 5:53

3 Answers 3

1

Using regex:

>>> import re
>>> s = "[greeting] Hello my friends [me] my name is John. [bow] nice to meet you."
>>> re.findall(r'\[[\w\s.]+\]|[\w\s.]+', s)
['[greeting]', ' Hello my friends ', '[me]', ' my name is John. ', '[bow]', ' nice to meet you.']

Edit:

>>> s =  "I can't see you"
>>> re.findall(r'\[.*?\]|.*?(?=\[|$)', s)[:-1]
["I can't see you"]
>>> s = "[greeting] Hello my friends [me] my name is John. [bow] nice to meet you."
>>> re.findall(r'\[.*?\]|.*?(?=\[|$)', s)[:-1]
['[greeting]', ' Hello my friends ', '[me]', ' my name is John. ', '[bow]', ' nice to meet you.'
Sign up to request clarification or add additional context in comments.

2 Comments

Wow thanks, this is really powerful. I was struggeling with regex to understand its syntax and could only extract the words between the brackets but not the rest.
I have a problem with characters like '^' and "'", If my sentence contains for example "I can't see you" it will be divided to "I can" and "t see you". and '^' is simply ignored.
1

The function you're after is .split(). The function accepts a delimiter as its argument and returns a list made by splitting the string at every occurrence of the delimiter. To split a string, using either "[" or "]" as a delimiter, you should use a regular expression:

import re
str = "[greeting] Hello [me] my name is John."
re.split("\]|\[", str)
# returns ['', 'greeting', ' Hello ', 'me', ' my name is John.']

This uses a regular expression to split the string.

\] # escape the right bracket
|  # OR
\[ # escape the left bracket

3 Comments

@Mehdi Your question wasn't exactly very clear, but I now see what you're after.
@Mehdi Is it important that the "[" and "]" (brackets) are kept?
Not that important, I can delimit my special words using [#word] then # will remain as a marker of special word.
0

I think can't be done in one line, you need first split by ], then [:

# Run in the python shell
sentence = "[greeting] Hello [me] my name is John."
for part in sentence.split(']')
  part.split('[')

# Output
['', 'greeting']
[' Hello ', 'me']
[' my name is John.']

Comments

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.