How to remove parentheses from all single words in a string?

6 vues (au cours des 30 derniers jours)
S H
S H le 5 Juin 2016
Commenté : S H le 8 Sep 2025
Example:
(G5)*((G6)*(G2+G4))+(G2)*(-(G4+G6)*(G1))+(G4)*(-(G6)*(G3))
Changes to:
G5*(G6*(G2+G4))+G2*(-(G4+G6)*G1)+G4*(-G6*G3)

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 5 Juin 2016
Modifié(e) : Azzi Abdelmalek le 5 Juin 2016
s='(G5)*((G6)*(G2+G4))+(G2)*(-(G4+G6)*(G1))+(G4)*(-(G6)*(G3))'
[i1,i2]=regexpi(s,'\<\([a-z0-9]+\>\)')
s([i1 i2])=[]
Or
s='(G5)*((G6)*(G2+G4))+(G2)*(-(G4+G6)*(G1))+(G4)*(-(G6)*(G3))'
out=regexprep(s,'\<\(([a-zA-Z0-9]+)\>\)','$1')

Plus de réponses (1)

Charles Kluepfel
Charles Kluepfel le 8 Sep 2025
Modifié(e) : Walter Roberson le 8 Sep 2025
I've just added to the File Exchange
Charles Kluepfel (2025). removeParentheses ( https://www.mathworks.com/matlabcentral/fileexchange/181983-removeparentheses ), MATLAB Central File Exchange. Retrieved September 8, 2025.
It was developed using chatGPT.
  1 commentaire
S H
S H le 8 Sep 2025
It works and is effectively removing all redundant parentheses.

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by