Python finding substring between certain characters using regex and replace()

Posted by jCuga on Stack Overflow See other posts from Stack Overflow or by jCuga
Published on 2011-01-07T04:18:27Z Indexed on 2011/01/07 4:53 UTC
Read the original article Hit count: 188

Filed under:
|
|
|

Suppose I have a string with lots of random stuff in it like the following:

strJunk ="asdf2adsf29Value=five&lakl23ljk43asdldl"

And I'm interested in obtaining the substring sitting between 'Value=' and '&', which in this example would be 'five'.

I can use a regex like the following:

 match = re.search(r'Value=?([^&>]+)', strJunk)
 >>> print match.group(0)
 Value=five
 >>> print match.group(1)
 five

How come match.group(0) is the whole thing 'Value=five' and group(1) is just 'five'? And is there a way for me to just get 'five' as the only result? (This question stems from me only having a tenuous grasp of regex)

I am also going to have to make a substitution in this string such such as the following:

 val1 = match.group(1)
 strJunk.replace(val1, "six", 1)    

Which yields:

 'asdf2adsf29Value=six&lakl23ljk43asdldl'

Considering that I plan on performing the above two tasks (finding the string between 'Value=' and '&', as well as replacing that value) over and over, I was wondering if there are any other more efficient ways of looking for the substring and replacing it in the original string. I'm fine sticking with what I've got but I just want to make sure that I'm not taking up more time than I have to be if better methods are out there.

© Stack Overflow or respective owner

Related posts about python

Related posts about regex