Replacing character between square brackets

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

Risposta accettata

Stephen23
Stephen23 il 2 Mar 2020
Modificato: Stephen23 il 2 Mar 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 Commenti
Stephen23
Stephen23 il 2 Mar 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];'

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Board games in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by