Replacing character between square brackets

A={'[ABC; DEF];[GHI; JKH];'; '[ZBC; YEF];[XHI; UKH];'}
I need to replace ";" only which are appearing within sqare brackets with "^"

 採用された回答

Stephen23
Stephen23 2020 年 3 月 2 日
編集済み: Stephen23 2020 年 3 月 2 日

0 投票

>> A = {'[ABC; DEF];[GHI; JKH];'; '[ZBC; YEF];[XHI; UKH];'}
A =
'[ABC; DEF];[GHI; JKH];'
'[ZBC; YEF];[XHI; UKH];'
>> B = regexprep(A,'(\[[^\]]+);([^\]]+\])','$1^$2')
B =
'[ABC^ DEF];[GHI^ JKH];'
'[ZBC^ YEF];[XHI^ UKH];'
or perhaps:
>> B = regexprep(A,'(?<!\]);(?!\[)','^')
B =
'[ABC^ DEF];[GHI^ JKH];'
'[ZBC^ YEF];[XHI^ UKH];'

6 件のコメント

Muhammad Rahil Rafiq
Muhammad Rahil Rafiq 2020 年 3 月 2 日
Thanks Stephen
Muhammad Rahil Rafiq
Muhammad Rahil Rafiq 2020 年 3 月 2 日
What if we have more than one ";" in a cell
e.g
>> A = {'[ABC; DEF; LMN];[GHI; JKH; OPQ; RST];'; '[ZBC; YEF; UVW; XYY; DFF; TRR];[XHI; UKH];'}
Stephen23
Stephen23 2020 年 3 月 2 日
"What if we have more than one ";" in a cell"
Then you need to specify the expected output. I cannot guess what you want.
Muhammad Rahil Rafiq
Muhammad Rahil Rafiq 2020 年 3 月 2 日
編集済み: Muhammad Rahil Rafiq 2020 年 3 月 2 日
My apologies.
Your earlier command is replacing only one last char ";".
I need to replace all ";" within square brackets
Stephen23
Stephen23 2020 年 3 月 2 日
>> A = {'[ABC; DEF; LMN];[GHI; JKH; OPQ; RST];'; '[ZBC; YEF; UVW; XYY; DFF; TRR];[XHI; UKH];'}
A =
'[ABC; DEF; LMN];[GHI; JKH; OPQ; RST];'
'[ZBC; YEF; UVW; XYY; DFF; TRR];[XHI; UKH];'
>> B = regexprep(A,'(\[[^\]]+\])','${strrep($1,'';'',''^'')}')
B =
'[ABC^ DEF^ LMN];[GHI^ JKH^ OPQ^ RST];'
'[ZBC^ YEF^ UVW^ XYY^ DFF^ TRR];[XHI^ UKH];'
Muhammad Rahil Rafiq
Muhammad Rahil Rafiq 2020 年 3 月 2 日
Thanks again!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および 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