Remove parenthesis and the contents inside from a string

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));

 Respuesta aceptada

A = 'abc (ABC)';
B = regexp(A,'^\w+','once','match')
B = 'abc'

5 comentarios

Mohammad Shahbazy
Mohammad Shahbazy el 12 de Mayo de 2021
Editada: Mohammad Shahbazy el 12 de Mayo de 2021
If we have something like this:
S = {'A(+229.16)AAAAAAAAW'
'AAA(+148.52)DETLRLW'
'AA(+155.89)ALPAAALW'};
How can we remove (+*) from middle?
I need to have the following array:
S = {'AAAAAAAAAW'
'AAADETLRLW'
'AAALPAAALW'};
Thanks in advance,
Moh
Perhaps one of these:
S = {'A(+229.16)AAAAAAAAW'
'AAA(+148.52)DETLRLW'
'AA(+155.89)ALPAAALW'};
A = regexprep(S,'[^A-Z]+','')
A = 3×1 cell array
{'AAAAAAAAAW'} {'AAADETLRLW'} {'AAALPAAALW'}
B = regexprep(S,'\([^)]+\)','')
B = 3×1 cell array
{'AAAAAAAAAW'} {'AAADETLRLW'} {'AAALPAAALW'}
Ivan Mich
Ivan Mich el 9 de Mzo. de 2023
Editada: Ivan Mich el 9 de Mzo. de 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?
"I mean to extract the only the characters that exist in brackets without the others."
A = 'abc (ABC)';
B = regexprep(A,{'.*\(','\).*'},'')
B = 'ABC'

Iniciar sesión para comentar.

Más respuestas (1)

KSSV
KSSV el 7 de En. de 2021
A = 'abc (ABC)' ;
idx = strfind(A,' (') ;
iwant = A(1:idx-1)

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 7 de En. de 2021

Comentada:

el 9 de Mzo. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by