Attach a string to each group of substrings separated by a character set

Imagine I have a line

Newton = 'kg*m/s^2'

      

and I need this:

NewtonMupad = 'unit::kg*unit::m/unit::s^2'

      

Is there an easy way to detect each physical unit and attach to it unit::

?
... It can be assumed that each unit is separated by a symbol /

, *

or exponent ^2

or ^3

.

Now I have used several regexes like

x = regexp(Newton ,'*','split')
y = regexp(Newton ,'/','split')
z = regexp(Newton ,'^','split')

      

and i can create the string i need with a loop. But I am wondering if there is a simpler and faster solution using Matlab?

+3


source to share


1 answer


You can use regexprep

:



>> Newton = 'kg*m/s^2'
>> regexprep(Newton,'(([a-zA-Z]+)(*|/|\^|$))', 'unit::$1')

ans = 

unit::kg*unit::m/unit::s^2

      

+4


source







All Articles