Changing letters to other letters with regexprep

4 ビュー (過去 30 日間)
Ryoma Kawakami
Ryoma Kawakami 2018 年 9 月 28 日
コメント済み: Walter Roberson 2018 年 9 月 28 日
I'm trying to change letters in a string to different ones with one line of code. I simplified my code down to the following:
regexprep('ab',{'a','b'},{'b','c'});
Ideally, this would output 'bc'. Currently, regexprep runs through the string twice and ends up outputting 'cc'.
Is there any way to prevent this from happening? Is it possible with regexprep? Also, I would prefer to keep the letters as strings, as I'm planning on using the 'preservecase' condition.

採用された回答

Stephen23
Stephen23 2018 年 9 月 28 日
編集済み: Stephen23 2018 年 9 月 28 日
You don't need regexprep, using ismember and indexing is simpler:
>> str = 'a':'z'
str = abcdefghijklmnopqrstuvwxyz
>> old = 'bcd';
>> new = 'abc';
>> [X,Y] = ismember(str,old);
>> str(X) = new(Y(X))
str = aabcefghijklmnopqrstuvwxyz
Note that this works efficiently for any characters, not just a limited subset. In order to use regexprep you would probably need to use a dynamic replacement expression.
  1 件のコメント
Walter Roberson
Walter Roberson 2018 年 9 月 28 日
Well, any characters representable in the first 65536 code points ;-) If you have codepoints beyond that, such as Linear B or CJK Unified Ideograms like ? then you would need more work.

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

その他の回答 (2 件)

OCDER
OCDER 2018 年 9 月 28 日
Note that regexprep will replace things Sequentially.
regexprep('ab',{'a','b'},{'b','c'});
ab -> bb -> cc
To fix, reverse order like this:
regexprep('ab',{'b', 'a'},{'c', 'b'});
ab -> ac -> bc
  2 件のコメント
Ryoma Kawakami
Ryoma Kawakami 2018 年 9 月 28 日
I apologize for not mentioning what I exactly planned to do after this.
I want regexprep (or something similar) to work for any shift in letters (such as 'bcd' to 'abc'), which means that reversing the order of evaluation wouldn't work.
OCDER
OCDER 2018 年 9 月 28 日
replace('a b c', {'a', 'b', 'c'}, {'c', 'a', 'b'})

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


Walter Roberson
Walter Roberson 2018 年 9 月 28 日
old = 'bcd';
new = 'abc';
lookup = char(0:255);
lookup(old+1) = new;
str = 'a':'z'
str = lookup(str+1)
You do not need to add 1 if you are willing to omit the possibility of char(0) being in the string.
  2 件のコメント
Stephen23
Stephen23 2018 年 9 月 28 日
編集済み: Stephen23 2018 年 9 月 28 日
Interesting. This will not work for characters >255 (or whatever limit you pick).
Walter Roberson
Walter Roberson 2018 年 9 月 28 日
Correct. You can extend it to max(str) easily, though.

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

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by