Replacing strings with varying length and numbers

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 件のコメント

Adam Danz
Adam Danz 2019 年 8 月 24 日
編集済み: Adam Danz 2019 年 8 月 24 日
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
Walter Roberson 2019 年 8 月 24 日
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
Daniel Schmidt 2019 年 8 月 25 日
The numbers are rather simple.
Only whole numbers smaller than 500, so no exponents necessary.
Also only positive numbers, and neither minus nor plus signs.
For non whole numbers, they are allways wriiten as 0.x with 1 decimal digit.
And there are only non whole numbers smaller 1, so never decimal digits for number greater zero.
A zero is indicated by just 0.
Adam Danz
Adam Danz 2019 年 8 月 25 日
It sounds like you just need to plug the values into the sprintf() command.
Walter Roberson
Walter Roberson 2019 年 8 月 25 日
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')

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2019 年 8 月 25 日

0 投票

>> str = 'Phase1_525kV_4km_100m_0.5ohm';
>> out = regexprep(str,'\d+\.?\d*[a-zA-Z]+','X')
out = Phase1_X_X_X_X

2 件のコメント

Daniel Schmidt
Daniel Schmidt 2019 年 8 月 25 日
Wow, very nice.
But what if I just want to replace one variable?
When I specifiy the unit 'm' that it replaces just the '100m'.
And if I choose 'ohm' that it replaces '0..5oohm'.
Is that possible.
Stephen23
Stephen23 2019 年 8 月 26 日
編集済み: Stephen23 2019 年 8 月 26 日
"But what if I just want to replace one variable?"
You can specify the unit:
>> str = 'Phase1_525kV_4km_100m_0.5ohm';
>> regexprep(str,'\d+\.?\d*m','X') % replace meters
ans = Phase1_525kV_4km_X_0.5ohm
>> regexprep(str,'\d+\.?\d*ohm','X') % replace ohms
ans = Phase1_525kV_4km_100m_X
Note that for robustness this should be the complete suffix+unit, followed by a lookaround assertion that checks if the following character is '_' or the end of the string, other 'm' can be ambiguously interpreted.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

2019 年 8 月 24 日

編集済み:

2019 年 8 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by