7

I have a string like :

searchString = "u:sads asdas asdsad n:sadasda as:adds sdasd dasd a:sed eee"

what I want is list :

["u:sads asdas asdsad","n:sadasda","as:adds sdasd dasd","a:sed eee"]

What I have done is :

values = re.split('\s', searchString)
mylist = []
word = ''
for elem in values:
  if ':' in elem:
    if word:
      mylist.append(word)
    word = elem
  else:
    word = word + ' ' + elem
list.append(word)
return mylist

But I want an optimized code in python 2.6 .

Thanks

1

2 Answers 2

13

Use regular expressions:

import re
mylist= re.split('\s+(?=\w+:)', searchString)

This splits the string everywhere there's a space followed by one or more letters and a colon. The look-ahead ((?= part) makes it split on the whitespace while keeping the \w+: parts

Sign up to request clarification or add additional context in comments.

Comments

1

You can use "look ahead" feature offered by a lot of regular expression engines. Basically, the regex engines checks for a pattern without consuming it when it comes to look ahead.

import re
s = "u:sads asdas asdsad n:sadasda as:adds sdasd dasd a:sed eee"
re.split(r'\s(?=[a-z]:)', s)

This means, split only when we have a \s followed by any letter and a colon but don't consume those tokens.

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.