regex replace whole word

Hi,
How to change
'v1 = v1a;'
to
'v(1) = v1a;'
using regex or regexrep?
regexprep('v1 = v1a','\bv1\b','v(1)')
apparently does not work.

 Accepted Answer

Stephen23
Stephen23 on 1 Jun 2018
Edited: Stephen23 on 1 Jun 2018
>> regexprep('v1 = v1a','^([a-z]+)(\d+)','$1($2)')
ans = v(1) = v1a
Hopefully you are not constructing arbitrary strings of code for evaluating!

8 Comments

Hi Stephen, thanks, works nicely.
The "whole word" will be a valid variable name (can contain underscore). I a second place I need a string equivalent for the symbolic subs(s, old, new). Have an idea? Thanks, Bardo
"The "whole word" will be a valid variable name (can contain underscore)."
Valid variable names can also contain numbers: what happens to them? If you only want to match letters and underscore, then try this:
'^([A-Za-z_]+)(\d+)'
"I a second place I need a string equivalent for the symbolic subs(s, old, new). Have an idea?"
What are you actually trying to achieve with all of this? My idea would be to not write code that relies on creating strings and evaluating them.
Bardo
Bardo on 1 Jun 2018
Edited: Bardo on 1 Jun 2018
  1. I have to deal with symbolic equations partially coming from text input. So I have to do safe substitutions on text level.
  2. The Symbolic Toolbox is not directly capable of dealing with array elements like v(1), but allows to declare them as x = sym('x',[10,1]). Now you can do calculations like x(1)*2 yielding 2*x1. The final code must use the array element form, so I need the above conversion.
Here we go:
regexprep('v1 = v1a','\<v1\>','v(1)')
ans =
v(1) = v1a
What you have shown does not require regular expressions: you could just as easily use strrep for what you are doing, or even basic indexing and concatenation:
>> str = 'v1 = v1a';
>> str = ['v(1)',str(3:end)]
str = v(1) = v1a
What you wrote is not much different from that really, and does not use any of the benefits of regular expressions, so it is unclear why you want to use regexprep.
In contrast this will work for any variable name (not just v1), including underscores, and relies on the regular expression to match location in the string and different kinds of characters:
>> regexprep('v1 = v1a','^([A-Za-z_]+)(\d+)','$1($2)')
ans = v(1) = v1a
But it all depends on what you are trying to do, so without a clear specification, this is all just observations on what can be seen on this thread.
Thanks Stephen indeed, much appreciated!
I reckon the main requested feature - to replace only whole words matching the string - is well in the title but got lost in the discussion.
@Bardo: you should accept your answer, if it resolves your original question.

Sign in to comment.

More Answers (1)

Here we go:
>> regexprep('v1 = v1a','\<v1\>','v(1)')
ans =
v(1) = v1a
or as a strrep for whole words
function snew = subsname(s, old, new)
snew = regexprep(s,['\<', old,'\>'], new);

Categories

Products

Release

R2016a

Tags

Asked:

on 1 Jun 2018

Commented:

on 1 Jun 2018

Community Treasure Hunt

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

Start Hunting!