5

Can you please help me to get the substrings between two characters at each occurrence

For example to get all the substrings between "Q" and "E" in the given example sequence in all occurrences:

ex: QUWESEADFQDFSAEDFS

and to find the substring with minimum length.

1
  • Could you please review your question and make it a little bit readable, also providing more details? Thank you. Commented Apr 25, 2009 at 11:07

2 Answers 2

16
import re
DATA = "QUWESEADFQDFSAEDFS"

# Get all the substrings between Q and E:
substrings = re.findall(r'Q([^E]+)E', DATA)
print "Substrings:", substrings

# Sort by length, then the first one is the shortest:
substrings.sort(key=lambda s: len(s))
print "Shortest substring:", substrings[0]
Sign up to request clarification or add additional context in comments.

Comments

7

RichieHindle has it right, except that

substrings.sort(key=len)

is a better way to express it than that redundant lambda;-).

If you're using Python 2.5 or later, min(substrings, key=len) will actually give you the one shortest string (the first one, if several strings tie for "shortest") quite a bit faster than sorting and taking the [0]th element, of course. But if you're stuck with 2.4 or earlier, RichieHindle's approach is the best alternative.

Comments

Your Answer

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