Regular Expression pattern for matching a variable name after an operator (mathematical or logical) ?

Hello, i am new to regular expressions and using regexprep as well. I want to write a function which replaces names of variables with a given new name. The expression i used first was (^|\W)oldName(\W|$) and $1newName$2 as the replacement.
Unfourtunatly this will not replace expressions like variableA*variableA or variableB+variableB. The second factor or summand is not replaced so i have to do regexp() twice. Secondly i tried the expression (^*|\W)oldName(\W|$) which will replace the second factor in the example above, but will also replace the name in something like this: thisvariableA.
matchPattern = (^|\W)oldName(\W|$);
replacePattern = $1newName$2;
StringContent = 'oldName*oldName';
ContentNew = regexprep(StringContent , replacePattern , matchPattern);
So i am looking for a regexp pattern which matches 'oldName' also after an operator which follows after and before'oldName'. A way to match the given example would help.

 Réponse acceptée

>> regexprep('variableA*variableA','(?<=\W)\w+','newName')
ans = variableA*newName
>> regexprep('variableB+variableB','(?<=\W)\w+','newName')
ans = variableB+newName
Your specification is quite vague: do you want to replace all of the instances of variableX with newName, or only the one after the operator (as your title states)? To learn how to write regular expressions read this page very carefully, and refer to it all the time:
You might also be interested in downloading my FEX submission iregexp, which provides an interactive tool for experimenting with regular expressions and showing regexp's outputs as you type:
It is useful for quickly experimenting with and refining regular expressions.

7 commentaires

Hello, thanks for your answer This will help me to make first steps with regular expressions. I will try your interactive tool. Sorry, my example was not very clear. Yes i want to replace all instances of variableX in such an expression. I there a solution running regexprep just once?
>> regexprep('variableA*variableA','\w+','newName')
ans = newName*newName
@Stephen Cobeldick, incomplete
>> regexprep('AnyVar*variableA','\w+','newName')
ans =
'newName*newName'
@Kai Teschner: your specification is not very clear, but if you want to replace one specific name anywhere in the original string, as many times as it occurs, then just do this:
>> regexprep('AnyVar*variableA','variableA','newName')
ans = AnyVar*newName
(or the same thing using strrep).
If there is the possibility of conflict, i.e. the variable name might be part of a longer string that you do NOT want to replace, then you can do this (where I want to replace A but not inside the words And or AnyVar):
>> regexprep('A/A And AnyVar*A And A+A','\<A\>','B')
ans = B/B And AnyVar*B And B+B
As you can see there are many options and possibilities. It all depends on your specification, which so far is not precise enough: if you want more help, please make a really clear specification: the more precise you are, the happier you will be with the result!
Note that an alternative might be to use the symbolic math toolbox, which includes variable substitution as a basic operation... this might be a good option, as in general using string manipulations to work with mathematical equations and formulas is not very efficient.
@Stephen Cobeldick: Ok i am sorry. I think my bad English is the reason. Yes i want to replace specific names of variables within a .m-file content. The example you gave is right. I do not want to replace the name WITHIN another name. Like not replace
VariableA
in
ThisVariableA
Currently i am using the following patterns
matchPattern = (^|\W)oldName(\W|$);
replacePattern = $1newName$2;
ContentNew = regexprep(fileContent , replacePattern , matchPattern);
These work well so far, but i get problems on the already discussed operations between the same variables when there is no space between operator and variable name like
variableA+variableA.
only becomes
newName+variableA
"Yes i want to replace specific names of variables within a .m-file content"
Hopefully you are not doing some kind of meta-programing! You can simply use the MATLAB editor to search-and-replace variable names (it even does this intelligently, only replacing the whole variable name as you want).
"I do not want to replace the name WITHIN another name..."
Did you try the code I gave in my last example? It seems to do exactly what you are asking for:
>> str = 'VariableA*VariableB but not ThisVariableA VariableA+VariableA';
>> rgx = '\<VariableA\>';
>> rpl = 'newName';
>> regexprep(str,rgx,rpl)
ans = newName*VariableB but not ThisVariableA newName+newName
Thanks, @Stephen Cobeldick your example works. This helped me a lot. Regular Expressions are a very interesting topic.
Hopefully you are not doing some kind of meta-programing!
Haha, yes i am aware of search and replace functions and shortcuts, but my goal was to automize replacement as far as possible.

Connectez-vous pour commenter.

Plus de réponses (1)

You may wish to consider using the shift+enter option in the MATLAB editor.
Find where variableA is defined for the first time or anywhere it is on the left of the = sign. E.g:
function(variableA)
or
variableA = something
Put the mouse cursor in it.
Change name by typing.
A yellow dialog will appear saying shift+enter to rename everywhere.
Hit shift+enter
Done.

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by