Find and replace in a character array

Hey, I have this character array F='(B~C) + (~AB)'. How can I assess the array to locate all the terms that has a ~ before it and replace it with a ('). So ~C should be C'. Therefore I would like F to be F=(BC') + (A'B)

 採用された回答

Walter Roberson
Walter Roberson 2020 年 3 月 6 日

0 投票

F='(B~C) + (~AB)'
F = regexprep(F, '~(.)', '$1''')

その他の回答 (1 件)

BobH
BobH 2020 年 3 月 6 日
編集済み: BobH 2020 年 3 月 6 日

0 投票

See regexprep and remember that to get a single-quote it must be doubled up in the replacement string
F='(B~C) + (~AB)';
regexprep(F,'~(.)','$1''') % ~ plus next character into that character plus single quote
ans =
(BC') + (A'B)

カテゴリ

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

質問済み:

2020 年 3 月 6 日

編集済み:

2020 年 3 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by