http://d.puremagic.com/issues/show_bug.cgi?id=3136

           Summary: Incorrect and strange behavior of std.regexp.RegExp if
                    using a pattern with optional prefix and suffix longer
                    than 1 char
           Product: D
           Version: 2.030
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: major
          Priority: P2
         Component: Phobos
        AssignedTo: nob...@puremagic.com
        ReportedBy: marcellogn...@gmail.com


It seems like std.regexp.RegExp get confused if I try using a pattern with
optional prefix and suffix longer than 1 char.
An expression of the form ([A]{0,2})(C)([D]{0,2}) matches all off "AC", "BC",
"CD", "CE", "ACD", "BCE", "ABCDE", "C" (as expected).
An expression of the form ([AB]{0,2})(C)([DE]{0,2}) or
([AB]?[AB]?)(C)([DE]?[DE]?) fails (incorrectly and unexpectedly) in some of the
cases above (both "CD" and "CE", for example).

Here the code:
---
import std.regexp;
import std.stdio;

public
{
    static void main()
    {
        RegExp eTest;
        void SetExp(string pattern)
        {
            eTest=new RegExp(pattern,"g");
            std.stdio.writeln("Testing expression ",pattern);
          }
        void TryString(string s)
        {
            std.stdio.writeln("Trying on string\"",s,"\":");
            auto captures=eTest.exec(s);
            if(captures.length)
            {
                std.stdio.writeln("Success!");
                foreach(uint i,string capture;captures)
                    std.stdio.writeln(i,"): \"",capture,"\"");
            }
            else
            {
                std.stdio.writeln("Failure!");
            }
        }
        SetExp(r"([A]{0,2})(C)([D]{0,2})");
        TryString("AC");
        TryString("BC");
        TryString("CD");
        TryString("CE");
        TryString("ACD");
        TryString("BCE");
        TryString("ABCDE");
        TryString("C");
        TryString("F");
        SetExp(r"([AB]{0,2})(C)([DE]{0,2})");
        TryString("AC");
        TryString("BC");
        TryString("CD");
        TryString("CE");
        TryString("ACD");
        TryString("BCE");
        TryString("ABCDE");
        TryString("C");
        TryString("F");
        SetExp(r"([AB]?[AB]?)(C)([DE]?[DE]?)");
        TryString("AC");
        TryString("BC");
        TryString("CD");
        TryString("CE");
        TryString("ACD");
        TryString("BCE");
        TryString("ABCDE");
        TryString("C");
        TryString("F");
    }
}
---

Here the output:
---
Testing expression ([A]{0,2})(C)([D]{0,2})
Trying on string"AC":
Success!
0): "AC"
1): "A"
2): "C"
3): ""
Trying on string"BC":
Success!
0): "C"
1): ""
2): "C"
3): ""
Trying on string"CD":
Success!
0): "CD"
1): ""
2): "C"
3): "D"
Trying on string"CE":
Success!
0): "C"
1): ""
2): "C"
3): ""
Trying on string"ACD":
Success!
0): "ACD"
1): "A"
2): "C"
3): "D"
Trying on string"BCE":
Success!
0): "C"
1): ""
2): "C"
3): ""
Trying on string"ABCDE":
Success!
0): "CD"
1): ""
2): "C"
3): "D"
Trying on string"C":
Success!
0): "C"
1): ""
2): "C"
3): ""
Trying on string"F":
Failure!
Testing expression ([AB]{0,2})(C)([DE]{0,2})
Trying on string"AC":
Success!
0): "AC"
1): "A"
2): "C"
3): ""
Trying on string"BC":
Success!
0): "BC"
1): "B"
2): "C"
3): ""
Trying on string"CD":
Failure!
Trying on string"CE":
Failure!
Trying on string"ACD":
Success!
0): "ACD"
1): "A"
2): "C"
3): "D"
Trying on string"BCE":
Success!
0): "BCE"
1): "B"
2): "C"
3): "E"
Trying on string"ABCDE":
Success!
0): "ABCDE"
1): "AB"
2): "C"
3): "DE"
Trying on string"C":
Failure!
Trying on string"F":
Failure!
Testing expression ([AB]?[AB]?)(C)([DE]?[DE]?)
Trying on string"AC":
Success!
0): "AC"
1): "A"
2): "C"
3): ""
Trying on string"BC":
Success!
0): "BC"
1): "B"
2): "C"
3): ""
Trying on string"CD":
Failure!
Trying on string"CE":
Failure!
Trying on string"ACD":
Success!
0): "ACD"
1): "A"
2): "C"
3): "D"
Trying on string"BCE":
Success!
0): "BCE"
1): "B"
2): "C"
3): "E"
Trying on string"ABCDE":
Success!
0): "ABCDE"
1): "AB"
2): "C"
3): "DE"
Trying on string"C":
Failure!
Trying on string"F":
Failure!
---

Kind regards,
Marcello Gnani

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------

Reply via email to