Difference between revisions of "Regular Expression Pattern Matching Using !Match"
(→Regular Expression Pattern Matching) |
(→Regular Expression Pattern Matching) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=== Regular Expression Pattern Matching === | === Regular Expression Pattern Matching === | ||
− | The AskPlus !match function uses the Perl Compatible Regular Expression (PCRE) engine to perform | + | The AskPlus !match function uses the Perl Compatible Regular Expression (PCRE) engine to perform pattern matching. If a match is found, the function returns the position of the match in the string. If no match is found, zero is returned. The basic syntax of !match is: |
!match(string, pattern) | !match(string, pattern) | ||
Line 7: | Line 7: | ||
An example of using !match to find invalid email addresses is shown below: | An example of using !match to find invalid email addresses is shown below: | ||
− | !match(!trim(MEMBER-FILE.E-MAIL), "^[^ @]+@[ | + | !match(!trim(MEMBER-FILE.E-MAIL), "^[^ @]+@[a-zA-Z0-9-]{1,63}\.[a-zA-Z]{2,6}$") = 0 AND MEMBER-FILE.E-MAIL <> " " |
The above example will not find all invalid email addresses, but it will find those that don't match the pattern. The equals zero, means that the pattern was not matched. See below for an explanation of this pattern: | The above example will not find all invalid email addresses, but it will find those that don't match the pattern. The equals zero, means that the pattern was not matched. See below for an explanation of this pattern: | ||
{| class="wikitable" style="border-spacing: 2,2,2,2;" | {| class="wikitable" style="border-spacing: 2,2,2,2;" | ||
− | |Pattern ||Explanation of pattern | + | |'''Pattern''' ||'''Explanation of pattern''' |
|- | |- | ||
| ^ || Start the match at the beginning of the string | | ^ || Start the match at the beginning of the string | ||
Line 19: | Line 19: | ||
| @ || Match the @ character | | @ || Match the @ character | ||
|- | |- | ||
− | | [ | + | | [a-zA-Z0-9-]{1,63} || Match the domain name. Valid characters are letters, digits and the hyphen. Between 1 and 63 characters long. |
|- | |- | ||
− | | \. || Match a period (dot). | + | | \. || Match a period (dot). Because the dot has special meaning (any character), it must be escaped by the backslash. |
|- | |- | ||
− | | [ | + | | [a-zA-Z]{2,6} || Match the TLD. Only letters are valid. Between 2 and 6 characters long. |
|- | |- | ||
| $ || Match until the end of the string. The !trim is needed to ensure trailing blanks are ignored. | | $ || Match until the end of the string. The !trim is needed to ensure trailing blanks are ignored. |
Latest revision as of 17:01, 17 February 2023
Regular Expression Pattern Matching
The AskPlus !match function uses the Perl Compatible Regular Expression (PCRE) engine to perform pattern matching. If a match is found, the function returns the position of the match in the string. If no match is found, zero is returned. The basic syntax of !match is:
!match(string, pattern)
An example of using !match to find invalid email addresses is shown below:
!match(!trim(MEMBER-FILE.E-MAIL), "^[^ @]+@[a-zA-Z0-9-]{1,63}\.[a-zA-Z]{2,6}$") = 0 AND MEMBER-FILE.E-MAIL <> " "
The above example will not find all invalid email addresses, but it will find those that don't match the pattern. The equals zero, means that the pattern was not matched. See below for an explanation of this pattern:
Pattern | Explanation of pattern |
^ | Start the match at the beginning of the string |
[^ @]+ | Match at least one (+) character that is not a blank or the @ character ([^ @] is a negated character class) |
@ | Match the @ character |
[a-zA-Z0-9-]{1,63} | Match the domain name. Valid characters are letters, digits and the hyphen. Between 1 and 63 characters long. |
\. | Match a period (dot). Because the dot has special meaning (any character), it must be escaped by the backslash. |
[a-zA-Z]{2,6} | Match the TLD. Only letters are valid. Between 2 and 6 characters long. |
$ | Match until the end of the string. The !trim is needed to ensure trailing blanks are ignored. |
- An excellent site for testing regular expressions can be found here. Remember to switch the matching engine from Javascript to PCRE.
- There are many PCRE resources available online. This cheatsheet is very handy.