Replacing character between square brackets

1 次查看(过去 30 天)
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
>> 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 个评论
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];'

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Board games 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by