Reputation: 456
I am new to python and I have been trying to check and substitute a specific value for a string. To illustrate, let me paste the bit of code that I used:-
var1 = re.sub(r'\$this->getSomething()->getSomethingElse()->(.*?)', r'\1', var1)
What I am trying to do here is to replace the entire string(var1) with the value contained within (.*?). E.g if the string is in this format "$this->getSomething()->getSomethingElse()->__('Title')" then the new value for var1 should be "__('Title')." At the moment I can't figure out what's wrong with the code and I tried searching all over the place including stackoverflow but not to avail.
Note : This seems to work well though :-
value = re.sub(r"\$title", "$this->title", value)
I hope someone can help me with this problem or at least direct me in the right direction. Thanks in advance.
Upvotes: 1
Views: 118
Reputation: 77400
Parentheses are metacharacters, used for grouping. You also want to match literal parentheses, which means you need to escape some instances. Try:
var1 = re.sub(r'\$this->getSomething\(\)->getSomethingElse\(\)->(.*?)', r'\1', var1)
Note that this particular statement is equivalent to:
var1 = re.sub(r'\$this->getSomething\(\)->getSomethingElse\(\)->', '', var1)
and:
var1 = var1.replace('$this->getSomething()->getSomethingElse()->', '')
Upvotes: 1