Regular Expression problem
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----
Why automate? I do automated testing because there's only so much a human being can do and remain healthy. Sleep is a requirement. So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.
Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
thank you, this helps alot with future usage of regular expressions.
For the current task of validating a date format, I will stay with aqConvert.StrToDate as you both suggested.
