Download presentation
Presentation is loading. Please wait.
Published byMarlene Hood Modified over 9 years ago
1
Using Regular Expressions in Java for Data Validation Evelyn Brannock Jan 30, 2009
2
Definition They are usually used to give a concise description of a set, without having to list all elements A regular expression is an expression that describes a set of strings
3
Origin In the 1950s, mathematician Stephen Cole Kleene devised a way to describe and classify formal languages using his mathematical notation called regular sets
4
Using Regular Expressions for Data Validation
5
Regular Expressions ConstructMatches xThe character x [abc]a, b, or c (simple class) [^abc]Any character except a, b, or c (negation) [a-zA-Z]a through z or A through Z, inclusive (range) \dA digit: [0-9] \DA non-digit: [^0-9] \sA whitespace character \SA non-whitespace character: [^\s] \wA word character: [a-zA-Z_0-9] \WA non-word character: [^\w] *from the Sun Java API
6
Regular Expressions ConstructMatches X?X?X, once or not at all X*X*X, zero or more times X+X+X, one or more times X{n}X{n}X, exactly n times X{n,}X, at least n times X{n,m}X{n,m}X, at least n but not more than m times XYX followed by Y X|YX|YEither X or Y *from the Sun Java API
7
Examples Searching for a word Matching an email address Verifying the format of a telephone number Credit card pre-validation IP address determination Zip code format validation The regular expression built is determined by the definition of valid values for the field
8
Building a Regular Expression **Matching a Word** Words that start with t, with at least one additional letter, all lowercase t[a-z]+ What if the word can start with a lowercase or uppercase t? [t|T][a-z]+ What if no additional letters must follow (but they may)?
9
Building a Regular Expression **Validating and IP Address** What does an IP address look like? Are three digits required? Is 999.999.999.999 a valid IP? 25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?
10
Steps Whitespace will often need to be stripped Determine the valid values of the string Create the regular expression Compile the regular expression into a pattern Place the string you wish to validate into the Matcher class Does the string match the pattern?
11
Java Invocation Sequence Two Java classes required: import java.util.regex.Pattern; import java.util.regex.Matcher; Code: Pattern p = Pattern.compile("a*b"); Matcher m = p.matcher("aaaaab"); boolean b = m.matches();compilematchermatches
12
U.S. Zip Code What is a valid expression for a zip code? Let’s build the regular expression together using GGCCustomerZipCode.java
13
Further Reading Teach Yourself Regular Expressions in 10 Minutes by Ben Forta Teach Yourself Regular Expressions in 10 Minutes by Ben Forta Mastering Regular Expressions by Jeffrey Friedl Mastering Regular Expressions by Jeffrey Friedl Java Regular Expressions by Mehran Habibi Java Regular Expressions by Mehran Habibi Regular Expression Pocket Reference by Tony Stubblebine Regular Expression Pocket Reference by Tony Stubblebine Regular Expression Recipes by Nathan Good Regular Expression Recipes by Nathan Good
14
Questions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.