# How do I match nested parenthesis (brackets, or braces) with dynamic regular expressions?

53 Ansichten (letzte 30 Tage)
Dan am 17 Mär. 2014
Bearbeitet: Daniel Renjewski am 15 Mär. 2023
One can read all over the web how it is impossible to use regular expressions to match nexted parenthesis. However MATLAB has this cool feature called 'dynamic regular expressions' that allow one to insert some MATLAB code to do all kinds of special 'gymnastics'. Is there a way to use this feature to count instances of parenthesis and, in turn, find their matches? Consider the following string:
g = 'asdf (( dwer e: ( asdedsdskek))::)asd fg ( qwe 4 dfy5 57) q34 dqa5';
or
g = 'asdf (( (dwer ) e: ( asdedsdskek))::)asd fg ( qwe 4 dfy5 57) q34 dqa5';
or
g = 'asdf ( dwer e: )asd fg ( qwe 4 dfy5 57) q34 dqa5';
Specifically, my need is only to match the first left parenthesis with its partner but one would think the more general solution of matching all sets of parenthesis is feasible with dynamic regular expressions. If anyone can help with this, it would be much appreciated.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Stephen23 am 1 Apr. 2020
Bearbeitet: Stephen23 am 1 Apr. 2020
This matches the outer-most matched pair of parentheses:
>> str = 'asdf (( dwer e: ( asdedsdskek))::)asd fg ( qwe 4 dfy5 57) q34 dqa5';
>> fun = @(s)sprintf('.{%d}',find(cumsum((s==')' )-(s=='('))>0,1,'first'));
>> out = regexp(str,'\((??@fun(\$''))','match')
out =
'(( dwer e: ( asdedsdskek))::)' '( qwe 4 dfy5 57)'
##### 2 KommentareKeine anzeigenKeine ausblenden
Dan am 1 Apr. 2020
Wow ... spending your covid-19 quarentine perusing old MATLAB questions? I appreciate the effort.
Andreas Bernatzky am 6 Apr. 2020
hahah thanks Stephen, I needed that too :)

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (2)

Walter Roberson am 17 Mär. 2014
It might be possible, but it will not be easy.
The regular expressions supported by MATLAB are very similar to the regular expressions supported by Perl.
Here is one way to use Perl just to count to see if parens are matched:
In pattern matching in Perl in which you are trying to balance pairs, see
and the (?PARNO) construct described at
The (?PARNO) and recurse constructs are not supported by MATLAB.
You just might be able to use the dynamic expressions to invoke a function that names itself inside of a dynamic expression, thus achieving recursion.
Warning: you will spend a lot of time getting it right. It would be much easier to write some code that did the analysis then to try to use regular expressions for it.
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Dan am 18 Mär. 2014
Thanks, Walter ... I ended up pulling in the string and writing a little MATLAB subroutine to do the task so I'm not motivated to research your references. Hopefully someone else can use the information in the future. Dan

Melden Sie sich an, um zu kommentieren.

Daniel Renjewski am 15 Mär. 2023
Bearbeitet: Daniel Renjewski am 15 Mär. 2023
I have got a similar problem as I wanted to identify fractions in the string of an equation to replace it with proper latex code. The following function gives you the position of all pairs of open and closing brackets with their respective position in the string, assuming there are indeed only pairs.
str = 'asdf (( dwer e: ( asdedsdskek))::)asd fg ( qwe 4 dfy5 57) q34 dqa5'
str = 'asdf (( dwer e: ( asdedsdskek))::)asd fg ( qwe 4 dfy5 57) q34 dqa5'
br = detect_brackets(str)
br = 4×2
17 30 7 31 6 34 42 57
for idx = 1:size(br,1)
display(str(br(idx,1):br(idx,2)))
end
( asdedsdskek) ( dwer e: ( asdedsdskek)) (( dwer e: ( asdedsdskek))::) ( qwe 4 dfy5 57)
function [oc] = detect_brackets(str)
oc = [];
% find all opening and closing brackets in the string
op=strfind(str,'(');
cl=strfind(str,')');
% search for pairs until all are identified
while ~isempty(op | cl)
% find opening bracket for first closing bracket
idx = find(op < cl(1),1,'last');
% append this pair to function output
oc = [oc;op(idx) cl(1)];
% remove found opening bracket from vector
op(idx) = [];
% remove found closing bracket from vector
cl(1) = [];
end
end
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Whos finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by