Replacing strings with varying length and numbers
Show older comments
I want to replace strings in varying length an numeric values.
I know that the regexprep function is very powerfull in modifying strings, but all its syntax is very complex for me.
My strings are structured like this:
'Phase1_525kV_4km_100m_0.5ohm'
Now I want to be able to replace each number and its subsequent unit up to the unterscore symbol.
So I need something that replaces all numbers in front of for exmaple km, but the length of the numeric values in front of the units are not allways the same.
This should also include values for not whole numbers like '0.5ohm' which should also be replaced completele.
What is the right syntax to use for the regexprep function?
thanks
5 Comments
We could certainly help you out with that but is that the best strategy for what you're doing? If you're trying to plug a set of values into a string with set units, perhaps this approach is simpler.
phase = 1;
kv = 525;
km = 4;
m = 100;
ohm = 0.5;
newString = sprintf('Phase%d_%dkV_%dkm_%dm_%.1fohm',phase,kv,km,m,ohm);
Use %d or %.0f to insert integers.
Use %.3f to use 3 decimal places.
Walter Roberson
on 24 Aug 2019
What exact syntax is permitted for the non whole numbers?
- are leading negative signs possible?
- are leading positive signs possible?
- if there is a decimal point, then will it always be followed by at least one digit? For example is 5.kW a possibility?
- if there is a decimal point, then will it always be proceeded by at least one digit? For example is .5kW a possibility?
- Is . by itself a permitted way to write 0?
- if there is an exponent indication then will it always be proceeded by a decimal point? For example is 5E3W a possibility? Will it always be preceded by at least one digit? Is E0 a permitted way of writing 0 for example?
- which exponent indicator characters are permitted? 'E' and 'e'? 'D' and 'd'? F or G?
- are negative exponent permitted?
- is explicit + in an exponent permitted?
- how many exponent characters are permitted?
Daniel Schmidt
on 25 Aug 2019
Adam Danz
on 25 Aug 2019
It sounds like you just need to plug the values into the sprintf() command.
Walter Roberson
on 25 Aug 2019
S = 'Phase1_525kV_4km_100m_0.5ohm';
newohm1 = '17';
newohm2 = '0.8';
newS1 = regexprep(S, '(0\.\d|\d+)(?=ohm)', newohm1, 'once')
newS2 = regexprep(newS1, '(0\.\d|\d+)(?=ohm)', newohm2, 'once')
Accepted Answer
More Answers (0)
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!