Remove parenthesis and the contents inside from a string

53 visualizzazioni (ultimi 30 giorni)
Is there a neat way to remove a parenthesis and the contents inside from a string. For example the string
A = 'abc (ABC)'
% how to extract 'abc' from A, get rid of ' (ABC)' including the leading whitespace?
One cumbersome solution is:
temp = strsplit(A,'(');
B = strtrim(temp(1));

Risposta accettata

Stephen23
Stephen23 il 7 Gen 2021
A = 'abc (ABC)';
B = regexp(A,'^\w+','once','match')
B = 'abc'
  5 Commenti
Ivan Mich
Ivan Mich il 9 Mar 2023
Modificato: Ivan Mich il 9 Mar 2023
Excuse me I have a question.. how could you do the inverse of this??
I mean to extract the only the characters that exist in brackets without the others.
for example:
input : A = 'abc (ABC)';
output B = 'ABC'
could you please help me?
Stephen23
Stephen23 il 9 Mar 2023
"I mean to extract the only the characters that exist in brackets without the others."
A = 'abc (ABC)';
B = regexprep(A,{'.*\(','\).*'},'')
B = 'ABC'

Accedi per commentare.

Più risposte (1)

KSSV
KSSV il 7 Gen 2021
A = 'abc (ABC)' ;
idx = strfind(A,' (') ;
iwant = A(1:idx-1)

Categorie

Scopri di più su Characters and Strings 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