Regular Expressions in ColdFusion Applications Dave Fauth DOMAIN technologies

Download Report

Transcript Regular Expressions in ColdFusion Applications Dave Fauth DOMAIN technologies

Regular Expressions in ColdFusion Applications

Dave Fauth DOMAIN technologies [email protected]

Knowledge Engineering : Systems Integration : Web Development : Training

Regular Expressions

• • • •

Small language in itself to perform pattern matching and text manipulation Used for client side validation, server side manipulation and virtually any other task requiring string matching and manipulation Enhanced in CF 4.0 to include REFindNoCase and REReplaceNoCase Available in CF Studio, CF 4.x, and JavaScript

Knowledge Engineering : Systems Integration : Web Development : Training

2

CF 4.x supported statements

• REFind • REFindNoCase • REReplace • REReplaceNoCase

Knowledge Engineering : Systems Integration : Web Development : Training

3

REFind

• Returns the position of the regular expressions first occurrence in a block of text • Case Sensitive • REFind(reg_expression,string [,start] [,returnsubexpression] #tmpLoc#

Knowledge Engineering : Systems Integration : Web Development : Training

4

REFindNoCase

• Returns the position of the regular expressions first occurrence in a block of text • Case Insensitive • REFindNoCase(reg_expression,string [,start] [,returnsubexpression] #tmpLoc#

Knowledge Engineering : Systems Integration : Web Development : Training

5

REReplace

• Return a string with the regular expression replaced with a substring in the specified scope • Case Sensitive • ReReplace(string,reg_expression,substring [,scope]) #tmpLoc#

Knowledge Engineering : Systems Integration : Web Development : Training

6

REReplaceNoCase

• Return a string with the regular expression replaced with a substring in the specified scope • Case Insensitive • ReReplaceNoCase(string,reg_expression,substring [,scope]) #tmpLoc#

Knowledge Engineering : Systems Integration : Web Development : Training

7

Single Character Matching

• Match a single character • Extensive set of rules for doing single character matching • Rules include:  Special Characters are: + * ? . [ ^ $ ( ) { | \     Any character not a special character matches itself A backslash escapes a special character A period matches any character except the newline A set of characters in brackets [] is a one character RE that matches any of the characters in the set. [AKM] matches A or K or M

Knowledge Engineering : Systems Integration : Web Development : Training

8

Single Character Matching Cont.

• Rules Cont.

 Any regular expression can be followed by {m,n} forces a match of m through n occurrences of the preceding regular expression. Example a{2,4} = aa, aaa, aaaa  A range of characters can be indicated with a dash. Example [A-Z] matches all uppercase letters. If the first character of the set is a ^, the RE matches any character except those in the set. I.e. [^AEIOU] matches all uppercase consonants

Knowledge Engineering : Systems Integration : Web Development : Training

9

Multi-Character Regular Expressions

• You can use the following rules to build a multi-character regular expressions:   Parentheses group parts of regular expressions together into grouped sub-expressions that can be treated as a single unit. For example, (ha)+ A one-character regular expression or grouped sub expressions followed by an asterisk (*) matches zero or more occurrences of the regular expression. For example, [a-z]* matches zero or more lower-case characters.

 A one-character regular expression or grouped sub expressions followed by a question mark (?) matches zero or one occurrences of the regular expression. For example, xy?z matches either "xyz" or "xz".

Knowledge Engineering : Systems Integration : Web Development : Training

10

Multi-Character cont.

   The concatenation of regular expressions creates a regular expression that matches the corresponding concatenation of strings. For example, [A-Z][a-z]* matches any capitalized word. The OR character (|) allows a choice between two regular expressions. For example, jell(y|ies) matches either "jelly" or "jellies". Braces ({}) are used to indicate a range of occurrences of a regular expression, in the form {m, n} where m is a positive integer equal to or greater than zero indicating the start of the range and n is equal to or greater than m, indicating the end of the range. For example, (ba){0,3} matches up to three pairs of the expression "ba".

Knowledge Engineering : Systems Integration : Web Development : Training

11

Character Classes

• Special Commands that can take the place of character ranges.

• CF uses double brackets [[alpha]] • Cold Fusion supports the following character classes: • • • • • alpha upper lower digit Alnum Za-z0-9].

Matches any letter. Same as [A-Za-z].

Matches any upper-case letter. Same as [A-Z].

Matches any lower-case letter. Same as [a-z].

Matches any digit. Same as [0-9].

Matches any alphanumeric character. Same as [A-

Knowledge Engineering : Systems Integration : Web Development : Training

12

Character Classes cont.

• • • • • • Xdigit - Matches any hexadecimal digit. Same as [0-9A-Fa-f].

Space - Matches a tab, new line, vertical tab, form feed, carriage return, or space.

Print - Matches any printable character.

punct - Matches any punctuation character, that is, one of ! ` # S % & ` ( ) * + , - . / : ; < = > ? @ [ / ] ^ _ { | } ~ graph - Matches any of the characters defined as a printable character except those defined to be part of the space character class.

cntrl - Matches any character not part of the character classes [:upper:], [:lower:], [:alpha:], [:digit:], [:punct:], [:graph:], [:print:], or [:xdigit:].

Knowledge Engineering : Systems Integration : Web Development : Training

13

Character Classes example

", "" , "ALL")> ]*>", "", "ALL")>

Knowledge Engineering : Systems Integration : Web Development : Training

14

Back Referencing

• Capability of regular expressions to remember a section of text and refer to it later • Parenthesis provide grouping for back references • Grouping is referred to using ‘\1’ through ‘\9’ • Expressions are counted from left to right  ex. “(a(bc)(d))   \1 = a(bc)(d) \2 = bc  \3 = d • Powerful for search and replace functions

Knowledge Engineering : Systems Integration : Web Development : Training

15

Back Referencing example

\2\4', "ALL")>

Knowledge Engineering : Systems Integration : Web Development : Training

16

Using Regular Expressions in Studio

• Extended find and replace in Studio and Homesite support Regular Expressions • Open the extended find or the extended replace dialog box. Check the regular expressions box. Type in your regular expression. The Studio RE engine evaluates the selected files and returns each matching pattern

Knowledge Engineering : Systems Integration : Web Development : Training

17

Example Uses of Regular Expressions

• Removing HTML Tags from Text • Retrieving Information from a page

refindnocase("]*>(.*)", pagetext, 1, "TRUE") REFindNoCase("[[:upper:]]{6}-[[:digit:]]{2} [[:digit:]]{4,6}",Body)

Knowledge Engineering : Systems Integration : Web Development : Training

18

When Not To Use Regular Expressions

• When it is easier to use something else…  Example: Rather than write: Write:

Knowledge Engineering : Systems Integration : Web Development : Training

19

Cold Fusion RE Limitation

• Limiting input string size  In CFML RegExp functions such as REFind and REReplace, large input strings (greater than approximately 20,000 characters) will cause a debug assertion failure and a regular expression error will be reported. To avoid this, break up your input into smaller chunks as illustrated in the following example. Here the variable input has a size greater than 50000. "#chr(10)#", "#chr(10)#", "#chr(10)#",

Knowledge Engineering : Systems Integration : Web Development : Training

20

Resources

• Javascript    http://developer.netscape.com/library/documentation/communicator/jsguide/ regexp.htm

http://developer.netscape.com/docs/examples/javascript/regexp/overview.ht

m/documentation/communicator/jsguide/regexp.htm

JavaScript Bible 3rd Edition by Danny Goodman • CF Studio  file:///C|/PROGRAM FILES/ALLAIRE/COLDFUSION STUDIO4/Help/Developing_Web_Applications_with_ColdFusion/08_Regular_E xpressions • Cold Fusion   Advanced Cold Fusion 4.0 Application Development by Ben Forta CF-Talk Mailing List • [email protected]

• General  An excellent reference on regular expressions is Mastering Regular Expressions, Jeffrey E. F. Friedl. O'Reilly & Associates, Inc., 1997. ISBN: 1 56592-257-3, http://www.oreilly.com.

Knowledge Engineering : Systems Integration : Web Development : Training

21