Replacing strings with varying length and numbers

4 ビュー (過去 30 日間)
Daniel Schmidt
Daniel Schmidt 2019 年 8 月 24 日
編集済み: Stephen23 2019 年 8 月 26 日
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 月 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 日
>> 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 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by