UNISA Chatter – Design patterns in C++ Part 6: Widgets … Validation and Regular Expressions using QT
See UNISA – Summary of 2010 Posts for a list of related UNISA posts. Continued from UNISA Chatter – Design patterns in C++ Part 5: Multi-Threading with QT.
IMPORTANT POINT: It is important to emphasize that the intent of these posts are to share my learning's as I dig through the last three subjects of my part-time UNISA studies. The posts by no means promote concepts or technologies … they are pure information sharing for fellow students … although the highlight that we explore more technologies and concepts that we typically prefer :)
QT and Environment Summary
The following is a summary of findings as I worked through the course related book. See the summary post for details on the book.
Terminology | Description | Example |
QRegExp | Implements Perl-style extended regular expression language. | QRegExp qre(“[\\s-\\+\\(\\)\\-]”); if (qre.exactMatch(…)) {…} |
QRegExpValidator | Uses QRegExp to validate an input string. |
Regular Expressions
Category | Expression | Description |
Special Characters | . \n \f \t \xhhhh | any character newline character form feed character tab character Unicode character defined by hexadecimal number, i.e. \xFFFF |
Quantifiers | + ? * {x,y} | 1 or more 0 or one 0 or more at least ‘x’ but no more that ‘y’ occurrences |
Character Sets | \s \S \d \D \w \W [a-x] [^a-x] | whitespace character non-whitespace character digit character (0-9) non-digit character any letter or digit or underscore (_) character non-word character characters in the range of a to x, excluding yz characters except in the range of a to x, i.e. y and z |
Anchoring | ^ $ \b \B | if the first character, indicates that the match starts at beginning of string Match must continue to end of string word boundary non-word boundary |
There are many, many more regular expressions. Consult your product documentation for more information.
Example of simple sample application checking for dates using the QT framework:
To conclude, let’s dissect the regular expression used for the date validation:
- The first character ^ indicates that the next match (19|20) starts at beginning of string.
- We either need a 19 or a 20 at the beginning of the string.
- Next we have two digit character (0-9).
- Next we have a range of valid characters, in this case minus, slash, space and dot.
- Next we have two possible combinations of digits.
- Either we have a zero, followed by a digit in the range of 1-9, i.e. 01 to 09.
- Or we have a one, followed by a zero, one or two, i.e. 10, 11 or 12.
- The last character $ indicates that the next match must continue to end of string.
- Next we have three possible combinations of digits.
- Either we have a zero, followed by a digit in the range of 1-9, i.e. 01 to 09.
- Or we have a one or a two, followed by a digit in the range of 0-9, i.e. 10 to 29.
- Or we have a three, followed by a zero or a one, i.e. 30 - 31.
If you need more examples, go to https://www.regular-expressions.info or other of the “many” regular expression repositories.