find string matching whole word

  • Follow


Hi,

I need to find some strings in a file, but it has to match only the whole word. I have some variables which I have to change but as some are variables like t, a, etc. If I do not use a whole word match everything is changed. The equations are created by the simbolic toolbox, so that searching for spaces between the variables is not a possibility.

I am doing it using fprintf and the command regexprep but I dont know how to make it to search only for whole words.

example:

F_dr = t*(gt-ha)+2*a-1.

I need to change t to t_1 and a to A2 but gt and ha should stay as they are:

F_dr = t_1*(gt-ha)+2*A2-1.
0
Reply nico 1/7/2011 4:27:05 PM

"nico cruz" <m.nicolas.cruz@mailbox.tu-berlin.de> wrote in message <ig7esp$d1a$1@fred.mathworks.com>...
> Hi,
> 
> I need to find some strings in a file, but it has to match only the whole word. I have some variables which I have to change but as some are variables like t, a, etc. If I do not use a whole word match everything is changed. The equations are created by the simbolic toolbox, so that searching for spaces between the variables is not a possibility.
> 
> I am doing it using fprintf and the command regexprep but I dont know how to make it to search only for whole words.
> 
> example:
> 
> F_dr = t*(gt-ha)+2*a-1.
> 
> I need to change t to t_1 and a to A2 but gt and ha should stay as they are:
> 
> F_dr = t_1*(gt-ha)+2*A2-1.

syms t gt ha a 
F_dr = t*(gt-ha)+2*a-1;

% Define the variables you want to substitute
syms t_1 A2
% Use subs
subs(F_dr,{t,a},{t_1,A2})

And don't use gt since it's a built-in matlab function.

Oleg
0
Reply Oleg 1/7/2011 4:56:04 PM


"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <ig7gj4$32o$1@fred.mathworks.com>...
> "nico cruz" <m.nicolas.cruz@mailbox.tu-berlin.de> wrote in message <ig7esp$d1a$1@fred.mathworks.com>...
> > Hi,
> > 
> > I need to find some strings in a file, but it has to match only the whole word. I have some variables which I have to change but as some are variables like t, a, etc. If I do not use a whole word match everything is changed. The equations are created by the simbolic toolbox, so that searching for spaces between the variables is not a possibility.
> > 
> > I am doing it using fprintf and the command regexprep but I dont know how to make it to search only for whole words.
> > 
> > example:
> > 
> > F_dr = t*(gt-ha)+2*a-1.
> > 
> > I need to change t to t_1 and a to A2 but gt and ha should stay as they are:
> > 
> > F_dr = t_1*(gt-ha)+2*A2-1.
> 
> syms t gt ha a 
> F_dr = t*(gt-ha)+2*a-1;
> 
> % Define the variables you want to substitute
> syms t_1 A2
> % Use subs
> subs(F_dr,{t,a},{t_1,A2})
> 
> And don't use gt since it's a built-in matlab function.
> 
> Oleg

Thank you for the post Oleg. I am working with the orginial poster - allow me to clarify the problem.

We need to work with symbolics in order to create some expressions, and we need to then convert the variable names to use for a different purpose, outside of matlab.

I liked your idea, because in our case it would mean simply converting the variable names before converting to strings (which we need as our output). However, our variable names need to be changed to experessions that are invalid as variables in matlab.

For example, here are two of our variables, and the expressions they need to be converted to:

'KHSTO'     ------>     'K_{HSTO}^{A}'
'T'             ------>     'T^{A}'

So, the original solution was to convert to strings, and then replace, but then 'T' is found in both of the above variables. And we can not convert to the desired variables with symbolics because they are invalid.

thanks again for any help.
0
Reply kavon 1/7/2011 5:25:21 PM

2 Replies
567 Views

(page loaded in 0.426 seconds)

Similiar Articles:













7/27/2012 10:15:42 AM


Reply: