Presentation is loading. Please wait.

Presentation is loading. Please wait.

Regular Expressions BKF03 Brian Ciccolo. Agenda Definition Uses – within Aspen and beyond Matching Replacing.

Similar presentations


Presentation on theme: "Regular Expressions BKF03 Brian Ciccolo. Agenda Definition Uses – within Aspen and beyond Matching Replacing."— Presentation transcript:

1 Regular Expressions BKF03 Brian Ciccolo

2 Agenda Definition Uses – within Aspen and beyond Matching Replacing

3 Whats a Regular Expression? In computing, regular expressions, also referred to as regex or regexp, provide a concise and flexible means for matching strings of text, such as particular characters, words, or patterns of characters. A regular expression is written in a formal language that can be interpreted by a regular expression processor, a program that either serves as a parser generator or examines text and identifies parts that match the provided specification. http://en.wikipedia.org/wiki/Regular_expression

4 Why Use a Regex? Validate data entry Example: Verify the format of a date field is mm/dd/yyyy Find/replace on steroids Example: Reformat phone numbers to (###) ###-####

5 Regex Use in Aspen Data validation o Date, time field input o Validation rules (new in 3.0 – see session TEC07) Find/replace on steroids o System Log filter o Field formatting

6 RegEx Examples Using Notepad++ Select the proper Search Mode Select this option for our examples

7 Matching – The Basics Literals - plain old text Classes ExampleDefinition [abc]a, b, or c [a-z]Any lowercase letter [a-zA-Z]Any lowercase or uppercase letter [0-9]Any digit, 0 through 9 [^a-zA-Z]Not a letter (could be a digit or punctuation)

8 Matching – Predefined Classes Predefined Class Definition.Any character \dAny digit: [0-9] \DAny non-digit: [^0-9] \sA whitespace character (space, tab, newline) \SA non-whitespace character: [^\s] \wA word character: [a-zA-Z_0-9] \WA non-word character (i.e., punctuation): [^\w]

9 Matching – Quantifiers QuantifierDefinition ? Matches 0 or 1 time (Not supported by Notepad++) +Matches 1 or more times *Matches 0 or more times {n,m} Matches at least n times but no more than m times (Not supported by Notepad++)

10 Matching – Greedy vs. Lazy Quantifiers are greedy by default – they match as many characters as possible Sometimes you want to match the fewest characters possible – enter lazy quantifiers QuantifierLazy Equivalent* ??? ++? **? * Not supported by Notepad++

11 Replacing – Groups Groups in the regex can be used in the replacement value Delimited with parentheses in the regex Identified with \n where n is the n th group in the original expression \0 represents the entire match (not supported in Notepad++)

12 Reformatting Dates Change mm/dd/yyyy to yyyy-mm-dd Regex: (\d+)/(\d+)/(\d+) Replacement: \3-\1-\2 Step 2 – pad the single digits! Regex: -(\d)([-"]) Replacement: -0\1\2

13 Reformatting Phone Numbers (v1) Wrap the area code in parentheses Regex: "(\d\d\d)- Replacement: "(\1) Ends with a space!

14 Reformatting Phone Numbers (v2) Strip punctuation (numbers only) Regex: \((\d+)\) (\d+)-(\d+) Replacement: \1\2\3

15 Reformatting Social Security Numbers Format SSN as ###-##-#### Do it in Aspen! Define a record in the Regular Expression Library table Set the regex on the Person ID field in the Data Dictionary

16 Define a Regular Expression Regex and format properties

17 Update the Data Dictionary Link to the regex

18 Verify the Results

19 Extras Wikipedia Entry http://en.wikipedia.org/wiki/Regular_expression http://en.wikipedia.org/wiki/Regular_expression Regular Expressions Cheat Sheet (V2) http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet Java regex support http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html Notepad++ text editor and regex support http://notepad-plus.sourceforge.net http://notepad-plus.sourceforge.net http://notepad-plus.sourceforge.net/uk/regExpList.php

20 Thank you. bciccolo@x2dev.com


Download ppt "Regular Expressions BKF03 Brian Ciccolo. Agenda Definition Uses – within Aspen and beyond Matching Replacing."

Similar presentations


Ads by Google