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.