Top Prev Next Up Down TEST

ExKappa

Example of the odd result of capturing parentheses which should be repeated twice


001| with Ada.Text_IO;
002| with Y2018.Text.Core; use Y2018.Text.Core;
003| with Y2018.Text.Core.Str; use Y2018.Text.Core.Str;
004| with Y2018.Text.Jets; use Y2018.Text.Jets;
005| with Y2018.Text.Jets.MatchPack;
006| with Y2018.Text.Jets.PatternPack;
007| --with DSECT;
008| procedure ExKappa is
009| __p:PatternPack.Pattern_AC:=PatternPack.compileM("^([\\w]+[\\s]?){2}"c);
010| __m : MatchPack.Match_TY;
011| __line:CFix:="Huey Dewey Louie Duck"c;
012| __nextPos:Integer;
013| __startPos:Integer:=1;
014| begin
015| __while PatternPack.matches(p,startPos,nextPos,line,m) loop
016| _____Ada.Text_IO.Put_Line ("---" & Integer'image(startPos) & " ---");
017| _____declare
018| ________r:I_A_ARRAY:=MatchPack.getMatch(m);
019| _____begin
020| ________for i in r'First(1) .. r'Last(1) loop
021| ___________Ada.Text_IO.Put_Line(Integer'image(i) & ">" & subIA(line,r(i)));
022| ________end loop;
023| _____end;
024| _____startPos:=nextPos;
025| __end loop;
026| __Ada.Text_IO.Put_Line ("*** Alternative two ***");
027| __startPos:=1;
028| __p:=PatternPack.compileM("^(?:([\\w]+[\\s]?)){2}"c);
029| __while PatternPack.matches(p,startPos,nextPos,line,m) loop
030| _____Ada.Text_IO.Put_Line ("---" & Integer'image(startPos) & " ---");
031| _____declare
032| ________r:I_A_ARRAY:=MatchPack.getMatch(m);
033| _____begin
034| ________for i in r'First(1) .. r'Last(1) loop
035| ___________Ada.Text_IO.Put_Line(Integer'image(i) & ">" & subIA(line,r(i)));
036| ________end loop;
037| _____end;
038| _____startPos:=nextPos;
039| __end loop;
040| __Ada.Text_IO.Put_Line ("*** Alternative three ***");
041| __startPos:=1;
042| __p:=PatternPack.compileM("^((?:[\\w]+[\\s]?){2})"c);
043| __while PatternPack.matches(p,startPos,nextPos,line,m) loop
044| _____Ada.Text_IO.Put_Line ("---" & Integer'image(startPos) & " ---");
045| _____declare
046| ________r:I_A_ARRAY:=MatchPack.getMatch(m);
047| _____begin
048| ________for i in r'First(1) .. r'Last(1) loop
049| ___________Ada.Text_IO.Put_Line(Integer'image(i) & ">" & subIA(line,r(i)));
050| ________end loop;
051| _____end;
052| _____startPos:=nextPos;
053| __end loop;
054| __Ada.Text_IO.Put_Line ("*** End of ExKappa ***");
055| end ExKappa;

Result

Running export LD_LIBRARY_PATH=lib;bin/exkappa in TEST directory.
Note ^([\\w]+[\\s]?){2} and that "Dewey" in the first iteration is in r(0), r(1) and r(2). We have the same result with "Duck" in the second iteration, although we have only one capturing parentheses in the pattern.

The question is what do we really want to capture ?

Alternative two uses non capturing parentheses in the pattern ^(?:([\\w]+[\\s]?)){2} and captures only the inner part.
Alternative three uses non capturing parentheses for the whole expression, ^((?:[\\w]+[\\s]?){2})

001| --- 1 ---
002| 0>Huey Dewey
003| 1>Huey Dewey
004| 2>Dewey
005| --- 12 ---
006| 0>Louie Duck
007| 1>Louie Duck
008| 2>Duck
009| *** Alternative two ***
010| --- 1 ---
011| 0>Huey Dewey
012| 1>Huey
013| 2>Dewey
014| --- 12 ---
015| 0>Louie Duck
016| 1>Louie
017| 2>Duck
018| *** Alternative three ***
019| --- 1 ---
020| 0>Huey Dewey
021| 1>Huey Dewey
022| --- 12 ---
023| 0>Louie Duck
024| 1>Louie Duck
025| *** End of ExKappa ***