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

조회 수: 4 (최근 30일)
S H
S H 2016년 6월 5일
댓글: S H 2025년 9월 8일
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)

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 6월 5일
편집: Azzi Abdelmalek 2016년 6월 5일
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')

추가 답변 (1개)

Charles Kluepfel
Charles Kluepfel 2025년 9월 8일
편집: Walter Roberson 2025년 9월 8일
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
S H
S H 2025년 9월 8일
It works and is effectively removing all redundant parentheses.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by