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

6 ビュー (過去 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 Center および File ExchangeString Parsing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by