0

I am trying to split a string in python to extract a particular part. I am able to get the part of the string before the symbol < but how do i get the bit after? e.g. the emailaddress part?

 >>> s = 'texttexttextblahblah <emailaddress>'
 >>> s = s[:s.find('<')]
 >>> print s

This above code gives the output texttexttextblahblah 

1
  • 1
    may be will be more correctly: s = s[s.find('<')+1:]? Commented Jan 31, 2013 at 16:51

3 Answers 3

3
s = s[s.find('<')+1:-1]

or

s = s.split('<')[1][:-1]
Sign up to request clarification or add additional context in comments.

1 Comment

Cheers. Works perfectly. Yeah nothing will ever follow the > in my data.
1

cha0site's and ig0774's answers are pretty straightforward for this case, but it would probably help you to learn regular expressions for times when it's not so simple.

import re
fullString = 'texttexttextblahblah <emailaddress>'
m = re.match(r'(\S+) <(\S+)>', fullString)
part1 = m.group(1)
part2 = m.group(2)

Comments

0

Perhaps being a bit more explicit with a regex isn't a bad idea in this case:

import re
match = re.search("""
    (?<=<)  # Make sure the match starts after a <
    [^<>]*  # Match any number of characters except angle brackets""", 
    subject, re.VERBOSE)
if match:
    result = match.group()

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.