Presentation is loading. Please wait.

Presentation is loading. Please wait.

Regular Expressions Theory and Practice Jeff Schoolcraft MDCFUG 12/13/2005.

Similar presentations


Presentation on theme: "Regular Expressions Theory and Practice Jeff Schoolcraft MDCFUG 12/13/2005."— Presentation transcript:

1 Regular Expressions Theory and Practice Jeff Schoolcraft MDCFUG 12/13/2005

2 Who am I? Jeff Schoolcraft Jeff Schoolcraft Senior Architect / Operations Manager at RGII Technologies. Senior Architect / Operations Manager at RGII Technologies. Speaker at Usergroups Speaker at Usergroups President WinProTeam Vienna Usergroup (.NET) President WinProTeam Vienna Usergroup (.NET) TDD Evangelist TDD Evangelist Tool guy Tool guy

3 What can you expect? “The gist” in 60 seconds or less. “The gist” in 60 seconds or less. Theory Theory Practical Usage Practical Usage Best Practices Best Practices Hands On Hands On A sermon A sermon Q & A Q & A

4 The Gist Regular Expressions (regex) describe patterns in strings and are often used for data validation, searching and text transformations. Regular Expressions (regex) describe patterns in strings and are often used for data validation, searching and text transformations.

5 Theory Basics A regular expression, often called a pattern, is an expression that describes a set of strings without actually listing its elements. Basics A regular expression, often called a pattern, is an expression that describes a set of strings without actually listing its elements. Say what? Say what? The set of strings {“dog”, “bog”, “fog”} can be described by this regular expression (regex): [bdf]og

6 Formal Language Theory A Regular Language is any language where all possible strings of that language can be described by a regular expression A Regular Language is any language where all possible strings of that language can be described by a regular expression

7 Formal Language Theory (cont’d) Regular expressions consist of constants and operators that denote sets of strings and operations over these sets, respectively. Given a finite alphabet Σ the following constants are defined: Regular expressions consist of constants and operators that denote sets of strings and operations over these sets, respectively. Given a finite alphabet Σ the following constants are defined: (empty set) ∅ denoting the set ∅ (empty set) ∅ denoting the set ∅ (empty string) ε denoting the set {ε} (empty string) ε denoting the set {ε} (literal character) a in Σ denoting the set {a} (literal character) a in Σ denoting the set {a}literal characterliteral character and the following operations: and the following operations: (concatenation) RS denoting the set { αβ | α in R and β in S }. For example {"ab", "c"}{"d", "ef"} = {"abd", "abef", "cd", "cef"}. (concatenation) RS denoting the set { αβ | α in R and β in S }. For example {"ab", "c"}{"d", "ef"} = {"abd", "abef", "cd", "cef"}. (alternation) R|S denoting the set union of R and S. (alternation) R|S denoting the set union of R and S. (Kleene star) R* denoting the smallest superset of R that contains ε and is closed under string concatenation. This is the set of all strings that can be made by concatenating zero or more strings in R. For example, {"ab", "c"}* = {ε, "ab", "c", "abab", "abc", "cab", "cc", "ababab",... }. (Kleene star) R* denoting the smallest superset of R that contains ε and is closed under string concatenation. This is the set of all strings that can be made by concatenating zero or more strings in R. For example, {"ab", "c"}* = {ε, "ab", "c", "abab", "abc", "cab", "cc", "ababab",... }.Kleene starsupersetKleene starsuperset http://en.wikipedia.org/wiki/Regular_expression

8 DEMO All binary numbers All binary numbers All binary numbers that start and end in 1 All binary numbers that start and end in 1 All binary number that have 00 any other bits followed by 111 and any other bits. All binary number that have 00 any other bits followed by 111 and any other bits.

9 Practical Usage In order of popularity In order of popularity Searching Searching Much nicer than * or % Much nicer than * or % String Manipulation String Manipulation Parsing Parsing Replacement Replacement Validation Validation Input validation Input validation Database check constraints Database check constraints

10 Best Practices The most important thing to remember: The most important thing to remember: Regular Expressions are greedy Regular Expressions are greedy Make the most explicit match possible Make the most explicit match possible Just because some implementations allow *? Don’t fall back on that. Just because some implementations allow *? Don’t fall back on that.

11 Hands On

12 A Sermon Some people develop a religious fascination with new tools & technologies (design patterns, regex, whatever). Some people develop a religious fascination with new tools & technologies (design patterns, regex, whatever). Use the tools that make the most sense for your problem/solution. Use the tools that make the most sense for your problem/solution.

13 Email Validation with REGEX? Are you kidding me? Are you kidding me? See email.regex See email.regex Multi-tiered approach, regex to test format, some code to test validity of email address. Multi-tiered approach, regex to test format, some code to test validity of email address.

14 Questions?

15 Further Resources Mastering Regular Expressions http://www.oreilly.com/catalog/regex/ Mastering Regular Expressions http://www.oreilly.com/catalog/regex/ http://www.oreilly.com/catalog/regex/ A website, 1000’s on google. http://www.regular-expressions.info/ A website, 1000’s on google. http://www.regular-expressions.info/ http://www.regular-expressions.info/ Me http://thequeue.net/blog/ http://regexadvice.com/blogs/jschoolcraft/ jeff@thequeue.net Me http://thequeue.net/blog/ http://regexadvice.com/blogs/jschoolcraft/ jeff@thequeue.net http://thequeue.net/blog/ http://regexadvice.com/blogs/jschoolcraft/ jeff@thequeue.net http://thequeue.net/blog/ http://regexadvice.com/blogs/jschoolcraft/ jeff@thequeue.net

16 Tools The Regulator (http://regex.osherove.com/) The Regulator (http://regex.osherove.com/)http://regex.osherove.com/ Expresso (http://www.ultrapico.com/Expresso.htm) Expresso (http://www.ultrapico.com/Expresso.htm)http://www.ultrapico.com/Expresso.htm


Download ppt "Regular Expressions Theory and Practice Jeff Schoolcraft MDCFUG 12/13/2005."

Similar presentations


Ads by Google