0
row=['alex','liza','hello **world**','blah']

i do i get everything in row[2] that is between the ** characters?

1
  • like msw said, dont use a chainsaw, regex is a chainsaw Commented Aug 2, 2010 at 19:39

4 Answers 4

3

You could do it by hand and search for the * , but regex work too.

print re.search(r'\*\*(.*)\*\*', 'hello **world**').group(1) # prints 'world'

You need to know exactly what you're looking for with regex, so think about what **asd**dfe** and similar edge cases should return.

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

Comments

1
import re
print re.findall(r"\*\*(.*)\*\*", row[2])

will give you a list of every match in row[2] which is between **. Fine tune the regex as necessary

Comments

1

Let's say that you don't know which element has the stars, you could use this:

row=['alex','liza','hello **world**','blah']
for staritem in (item for item in row if '**' in item):
    print(staritem)
    _,_, staritem = staritem.partition('**')
    staritem,_,_ = staritem.partition('**')
    print(staritem)

Comments

0

row[2].strip('*')

Don't use a chainsaw when a knife will do.

2 Comments

>>> row[2].strip('*') 'hello **world'
huh? how do you figure out which part is the one between the **?

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.