1

I am trying to learn python and regex at the same time and I am having some trouble in finding how to match till end of string and make a replacement on the fly.

So, I have a string like so:

ss="this_is_my_awesome_string/mysuperid=687y98jhAlsji"

What I'd want is to first find 687y98jhAlsji (I do not know this content before hand) and then replace it to myreplacedstuff like so:

ss="this_is_my_awesome_string/mysuperid=myreplacedstuff"

Ideally, I'd want to do a regex and replace by first finding the contents after mysuperid= (till the end of string) and then perform a .replace or .sub if this makes sense.

I would appreciate any guidance on this.

0

2 Answers 2

6

You can try this:

 re.sub(r'[^=]+$', 'myreplacedstuff', ss)

The idea is to use a character class that exclude the delimiter (here =) and to anchor the pattern with $

explanation:

[^=] is a character class and means all characters that are not =
[^=]+ one or more characters from this class
$ end of the string

Since the regex engine works from the left to the right, only characters that are not an = at the end of the string are matched.

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

3 Comments

Thanks so much for this. So, does the +$ look for end of string?
No only $, + is quantifier for [^=] that mean 1 or more times, I will edit a more detailed explanation soon.
Thanks so much for your detailed explanation - I have certainly learnt here. Accepted your answer.
1

You can use regular expressions:

>>> import re
>>> mymatch = re.search(r'mysuperid=(.*)', ss)
>>> ss.replace(mymatch.group(1), 'replacing_stuff')
'this_is_my_awesome_string/mysuperid=replacing_stuff'

You should probably use @Casimir's answer though. It looks cleaner, and I'm not that good at regex :p.

3 Comments

thanks so much for the alternative solution. I agree, Casimirs answer is a clean one liner. I agree - I am trying to be good with regex! I am quite bad at this!
@JamesW Well, you aren't alone :)
you should read more about regexps on sites like courses on the MIT and also practice on tutorial sites. I think it helps a lot to understand that Regexps are "simply" automata with cabalistical characters. In the end, it's expression is far less sophisticated than most programming language.. but the paradox is that it is harder to learn :-)

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.