Matching substrings with regexp

조회 수: 3 (최근 30일)
Tim
Tim 2014년 9월 11일
답변: per isakson 2014년 9월 11일
I'm trying to identify the subexpressions of a formula such as (real cases much longer):
y = ((a + b) * (c / d) + ( e + f ))
The subexpressions are always separated by pairs of parenthesis as in the example above. It seems like I should be able to use a regular expression with a rule like (intuitively) match "an initial left parenthesis, any characters (but no parenthesis), and a terminating right parenthesis" to get:
(a + b) (c / d) (e + f)
Can anybody suggest a regular expression to do this? Or any way other than a regular expression?

채택된 답변

Guillaume
Guillaume 2014년 9월 11일
편집: Guillaume 2014년 9월 11일
subexpressions = regexp(expression, '\([^()]+\)', 'match')
will match any sequence with two brackets enclosing one or more non-bracket.

추가 답변 (1개)

per isakson
per isakson 2014년 9월 11일
AFAIK: That is not possible with Matlab's regular expression. Recursion is needed. With Perl (and other languages it is possible). See http://perl.plover.com/yak/regex/samples/slide083.html
"any way other than" &nbsp See parse_equation

카테고리

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