Replacing character between square brackets
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Muhammad Rahil Rafiq
am 2 Mär. 2020
Kommentiert: Muhammad Rahil Rafiq
am 2 Mär. 2020
A={'[ABC; DEF];[GHI; JKH];'; '[ZBC; YEF];[XHI; UKH];'}
I need to replace ";" only which are appearing within sqare brackets with "^"
0 Kommentare
Akzeptierte Antwort
Stephen23
am 2 Mär. 2020
Bearbeitet: Stephen23
am 2 Mär. 2020
>> 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 Kommentare
Stephen23
am 2 Mär. 2020
>> 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];'
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!