Forum Discussion

S_Seydel's avatar
S_Seydel
Contributor
9 years ago

Regular Expression problem

Hi,

 

I try to use a regular expression to validate a specific date format, however I don't get it to work and would be thankful for any help.

 

TestComplete version: 12.0.122.7.

Script language: JScript.

 

Date format the regular expression pattern shall match:

%d.%m.%Y

 

Regular Expression pattern:

(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[012])\.(19|20)\d\d

 

Attempts to use the regular expression:

function testRegEx()
{
  // Attempt to use a regular exppression object with test method.
  var dateRegEx = new RegExp("(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[012])\.(19|20)\d\d", "g");
  var testBool1 = dateRegEx.test("23.09.2016");
  Log.Message(testBool1);

  // Attempt to use aqString.StrMatches
  var testBool2 = aqString.StrMatches("(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[012])\.(19|20)\d\d", "23.09.2016");
  Log.Message(testBool2);
}

testBool1 and testBool2 both result in false, however on regextester.com the regular expression works just fine.

 

I've read Regular Expressions Syntax in TestComplete's manual, but was not able to find the error.

 

 

Thank you very much in advance

S_Seydel

 

  • Possibly a "dirtier" way of doing this same thing without having to use Regular Expressions (cool thing, very useful, but confuses me a lot, too), is to try and use the aqConvert.StrToDateTime method wrapped in some sort of exception handling. So, you try the conversion, if it succeeds, no exception, you log a "success" result. If it fails, raises an exception, you log a "fail" result.

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Possibly a "dirtier" way of doing this same thing without having to use Regular Expressions (cool thing, very useful, but confuses me a lot, too), is to try and use the aqConvert.StrToDateTime method wrapped in some sort of exception handling. So, you try the conversion, if it succeeds, no exception, you log a "success" result. If it fails, raises an exception, you log a "fail" result.

    • S_Seydel's avatar
      S_Seydel
      Contributor

      Hi,

       

      thank you for your reply, your proposed workaround works just fine.

       

      Nevertheless I'm still curious why my regular expression doesn't work with TestComplete, because I wanted to use regular expressions more often in the future, for example with name mapping.

       

      Perhaps someone could shed some light into darkness.

      • HKosova's avatar
        HKosova
        SmartBear Alumni (Retired)

        To answer your original question, there are some gotchas with regular expressions.

         

        Firstly, because you write the regex as a string and this is JavaScript/JScript, you need to double the backslashes in the string, e.g. \. -> \\. and \d -> \\d. Similar to how you double the backslashes in paths --"C:\\MyFolder".

         

        Secondly, StrMatches uses a slightly different syntax than native JavaScript/JScript regexes. Specifically, "|" is non-greedy so "green|red" matches "greened" and "greered", not "green" and "red" as in native JavaScript/JScript regexes. To make it greedy you need to put the parentheses around the alternatives.

         

        The working version would be:

        function testRegEx()
        {
          // Attempt to use a regular exppression object with test method.
          var dateRegEx = new RegExp("(0[1-9]|[12][0-9]|3[01])\\.(0[1-9]|1[012])\\.(19|20)\\d\\d", "g");
          var testBool1 = dateRegEx.test("23.09.2016");
          Log.Message(testBool1);
        
          // Attempt to use aqString.StrMatches
          var testBool2 = aqString.StrMatches("(0[1-9])|([12][0-9])|(3[01])\\.(0[1-9])|(1[012])\\.(19)|(20)\\d\\d", "23.09.2016");
          Log.Message(testBool2);
        }

        But as already suggested, parsing the date is better than using a regex, because a regex won't catch invalid dates like February 31.