Presentation is loading. Please wait.

Presentation is loading. Please wait.

Regular Expressions in ColdFusion Applications Dave Fauth DOMAIN technologies Knowledge Engineering : Systems Integration : Web.

Similar presentations


Presentation on theme: "Regular Expressions in ColdFusion Applications Dave Fauth DOMAIN technologies Knowledge Engineering : Systems Integration : Web."— Presentation transcript:

1 Regular Expressions in ColdFusion Applications Dave Fauth DOMAIN technologies d.fauth@domain-tech.com Knowledge Engineering : Systems Integration : Web Development : Training

2 2 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

3 Knowledge Engineering : Systems Integration : Web Development : Training 3 CF 4.x supported statements REFind REFindNoCase REReplace REReplaceNoCase

4 Knowledge Engineering : Systems Integration : Web Development : Training 4 REFind Returns the position of the regular expressions first occurrence in a block of text Case Sensitive REFind(reg_expression,string [,start] [,returnsubexpression] #tmpLoc#

5 Knowledge Engineering : Systems Integration : Web Development : Training 5 REFindNoCase Returns the position of the regular expressions first occurrence in a block of text Case Insensitive REFindNoCase(reg_expression,string [,start] [,returnsubexpression] #tmpLoc#

6 Knowledge Engineering : Systems Integration : Web Development : Training 6 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#

7 Knowledge Engineering : Systems Integration : Web Development : Training 7 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#

8 Knowledge Engineering : Systems Integration : Web Development : Training 8 Single Character Matching Match a single character Extensive set of rules for doing single character matching Rules include: t Special Characters are: + * ?. [ ^ $ ( ) { | \ t Any character not a special character matches itself t A backslash escapes a special character t A period matches any character except the newline t 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

9 Knowledge Engineering : Systems Integration : Web Development : Training 9 Single Character Matching Cont. Rules Cont. t 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 t 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

10 Knowledge Engineering : Systems Integration : Web Development : Training 10 Multi-Character Regular Expressions You can use the following rules to build a multi-character regular expressions: t Parentheses group parts of regular expressions together into grouped sub-expressions that can be treated as a single unit. For example, (ha)+ t 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. t 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".

11 Knowledge Engineering : Systems Integration : Web Development : Training 11 Multi-Character cont. t 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. t The OR character (|) allows a choice between two regular expressions. For example, jell(y|ies) matches either "jelly" or "jellies". t 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".

12 Knowledge Engineering : Systems Integration : Web Development : Training 12 Character Classes Special Commands that can take the place of character ranges. CF uses double brackets [[alpha]] Cold Fusion supports the following character classes: alphaMatches any letter. Same as [A-Za-z]. upper Matches any upper-case letter. Same as [A-Z]. lower Matches any lower-case letter. Same as [a-z]. digitMatches any digit. Same as [0-9]. AlnumMatches any alphanumeric character. Same as [A- Za-z0-9].

13 Knowledge Engineering : Systems Integration : Web Development : Training 13 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:].

14 Knowledge Engineering : Systems Integration : Web Development : Training 14 Character Classes example Here is some text here is some bold text Here is italic text. "> ", "", "ALL")> ]*>", "", "ALL")>

15 Knowledge Engineering : Systems Integration : Web Development : Training 15 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 t ex. “(a(bc)(d)) t \1 = a(bc)(d) t \2 = bc t \3 = d Powerful for search and replace functions

16 Knowledge Engineering : Systems Integration : Web Development : Training 16 Back Referencing example \2 \4', "ALL")>

17 Knowledge Engineering : Systems Integration : Web Development : Training 17 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

18 Knowledge Engineering : Systems Integration : Web Development : Training 18 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)

19 Knowledge Engineering : Systems Integration : Web Development : Training 19 When Not To Use Regular Expressions When it is easier to use something else… t Example: Rather than write: Write:

20 Knowledge Engineering : Systems Integration : Web Development : Training 20 Cold Fusion RE Limitation Limiting input string size t 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.

21 Knowledge Engineering : Systems Integration : Web Development : Training 21 Resources Javascript t http://developer.netscape.com/library/documentation/communicator/jsguide /regexp.htm t http://developer.netscape.com/docs/examples/javascript/regexp/overview.ht m/documentation/communicator/jsguide/regexp.htm t JavaScript Bible 3rd Edition by Danny Goodman CF Studio t file:///C|/PROGRAM FILES/ALLAIRE/COLDFUSION STUDIO4/Help/Developing_Web_Applications_with_ColdFusion/08_Regular_E xpressions Cold Fusion t Advanced Cold Fusion 4.0 Application Development by Ben Forta t CF-Talk Mailing List cf-talk-request@houseoffusion.com General t 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.


Download ppt "Regular Expressions in ColdFusion Applications Dave Fauth DOMAIN technologies Knowledge Engineering : Systems Integration : Web."

Similar presentations


Ads by Google