title: Parsing texts with PerlRegEx in Delphi XE4
body: <p>I have to parse Spice file format and I chosen regular expression matching to parse the rows. For this I selected <a href="http://www.regular-expressions.info/download/TPerlRegEx.zip" rel="nofollow noreferrer">TPerlRegEx</a>. I trapped by <code>.subckt</code> processing. The SubCircuit object has the following attributes : name, node name list, parameter name-value pair list. I wrote some (the possible) test cases and few of them fail. The source code:</p>
  
  <pre><code>uses
    PerlRegEx
    ;
  
  {$R *.dfm}
  
  procedure TForm1.Button1Click(Sender: TObject);
  var
    aPRE : TPerlRegEx;
    i : integer;
  
  function getMatchingCaseIndex( subject_ : string ) : integer;
  const
    CONST_regexp_CubCircuitHeaders : array [0..3] of string =
      ( '.subckt\s([^\s]+)\s([\s\S]+)',
        '.subckt\s([^\s]+)\s([^\s]+)\s([^=]+=[^\s]+)',
        '.subckt\s([^\s]+)\s((?:[^\s]+)+)\sparams:\s([^\s]+\s[^\s]+)',
        '.subckt\s([^\s]+)\s((?:[^\s]+)+)\sparams:\s([^=]+=[^\s]+)' );
  var
    i : integer;
  begin
    result := -1;
    aPRE.subject := subject_;
    i := length( CONST_regexp_CubCircuitHeaders );
    while ( ( result &lt; 0 ) and ( i &gt; 0 ) ) do
    begin
      dec( i );
      aPRE.regEx := CONST_regexp_CubCircuitHeaders[i];
      if ( aPRE.match ) then
        result := i;
    end;
  end;
  
  begin
    aPRE := TPerlRegEx.create;
    try
      // Should return 0
      // Test cases without any parameter definition
      { 1}  i := getMatchingCaseIndex( '.subckt NAME NODE1' );
      { 2}  i := getMatchingCaseIndex( '.subckt NAME NODE1 NODE2' );
      // Should return 1
      // Test cases when 'params:' does not inserted (name=value parameter definiion form expected)
      { 3}  i := getMatchingCaseIndex( '.subckt NAME NODE1 name1=value1' );
      { 4}  i := getMatchingCaseIndex( '.subckt NAME NODE1 name1=value1 name2=value2' );
      { 5}  i := getMatchingCaseIndex( '.subckt NAME NODE1 NODE2 name1=value1' );
      { 6}  i := getMatchingCaseIndex( '.subckt NAME NODE1 NODE2 name1=value1 name2=value2' );
      // Should return 2
      // Test cases when 'params:' inserted (name value parameter definition form)
      { 7}  i := getMatchingCaseIndex( '.subckt NAME NODE1 params: name1 value1' );
      { 8}  i := getMatchingCaseIndex( '.subckt NAME NODE1 params: name1 value1 name2 value2' );
      { 9*} i := getMatchingCaseIndex( '.subckt NAME NODE1 NODE2 params: name1 value1' );
      {10*} i := getMatchingCaseIndex( '.subckt NAME NODE1 NODE2 params: name1 value1 name2 value2' );
      // Should return 3
      // Test cases when 'params:' inserted (name=value parameter definition form)
      {11}  i := getMatchingCaseIndex( '.subckt NAME NODE1 params: name1=value1 name2=value2' );
      {12}  i := getMatchingCaseIndex( '.subckt NAME NODE1 params: name1=value1 name2=value2' );
      {13*} i := getMatchingCaseIndex( '.subckt NAME NODE1 NODE2 params: name1=value1 name2=value2' );
      {14*} i := getMatchingCaseIndex( '.subckt NAME NODE1 NODE2 params: name1=value1 name2=value2' );
    finally
      aPRE.Free;
    end;
  end;
  </code></pre>
  
  <p>Test case (9,10) passes back 0 instead of 2 and test case (13,14) passes back 1 instead of 3. When two (or more) node name defined before the parameter list. How should I modify my regexps to all test cases pass back the right values?</p>