1 Form Validation. Validation  Validation of form data can be cumbersome using the basic techniques  StringTokenizer  If-else statements  Most of.

Slides:



Advertisements
Similar presentations
Regular Expression Original Notes by Song Guo. What Regular Expressions Are Exactly - Terminology a regular expression is a pattern describing a certain.
Advertisements

1 Regular Expressions & Automata Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
13-Jun-15 Regular Expressions in Java. 2 Regular Expressions A regular expression is a kind of pattern that can be applied to text ( String s, in Java)
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Regular Expressions in Java. Namespace in XML Transparency No. 2 Regular Expressions Regular expressions are an extremely useful tool for manipulating.
Regular Expressions in Java. Regular Expressions A regular expression is a kind of pattern that can be applied to text ( String s, in Java) A regular.
1 A Quick Introduction to Regular Expressions in Java.
Regular Expressions & Automata Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Regular expression. Validation need a hard and very complex programming. Sometimes it looks easy but actually it is not. So there is a lot of time and.
1 Overview Regular expressions Notation Patterns Java support.
Regular Expressions. String Matching The problem of finding a string that “looks kind of like …” is common  e.g. finding useful delimiters in a file,
An Introduction to TokensRegex
Applications of Regular Expressions BY— NIKHIL KUMAR KATTE 1.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Science: Text and Language Dr Andy Evans. Text analysis Processing of text. Natural language processing and statistics.
Last Updated March 2006 Slide 1 Regular Expressions.
MySql, CGI, PHP, XML, RE, CSS. 1. MySQL A popular, fast, easy-to-use RDBMS Developed, marketed, and supported by MySQL AB, a Swedish company Released.
Regular Expressions Dr. Ralph D. Westfall May, 2011.
Regular Expression Darby Tien-Hao Chang (a.k.a. dirty) Department of Electrical Engineering, National Cheng Kung University.
 Text Manipulation and Data Collection. General Programming Practice Find a string within a text Find a string ‘man’ from a ‘A successful man’
Regular Expressions in.NET Ashraya R. Mathur CS NET Security.
ADSA: RegExprs/ Advanced Data Structures and Algorithms Objective –look at programming with regular expressions (REs) in Java Semester 2,
Using Regular Expressions in Java for Data Validation Evelyn Brannock Jan 30, 2009.
CIS 451: Regular Expressions Dr. Ralph D. Westfall January, 2009.
Regular Expressions Regular expressions are a language for string patterns. RegEx is integral to many programming languages:  Perl  Python  Javascript.
Perl and Regular Expressions Regular Expressions are available as part of the programming languages Java, JScript, Visual Basic and VBScript, JavaScript,
BTANT129 w61 Regular expressions step by step Tamás Váradi
1 CSC 594 Topics in AI – Text Mining and Analytics Fall 2015/16 4. Document Search and Regular Expressions.
Regular Expressions CSC207 – Software Design. Motivation Handling white space –A program ought to be able to treat any number of white space characters.
Regular Expression in Java 101 COMP204 Source: Sun tutorial, …
Regular Expressions.
BY Sandeep Kumar Gampa.. What is Regular Expression? Regex in.NET Regex Language Elements Examples Regular Expression API How to Test regex in.NET Conclusion.
Regular Expressions – An Overview Regular expressions are a way to describe a set of strings based on common characteristics shared by each string in.
 2003 Jeremy D. Frens. All Rights Reserved. Calvin CollegeDept of Computer Science(1/8) Regular Expressions in Java Joel Adams and Jeremy Frens Calvin.
VBScript Session 13.
Overview A regular expression defines a search pattern for strings. Regular expressions can be used to search, edit and manipulate text. The pattern defined.
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Module 6 – Generics Module 7 – Regular Expressions.
Regular Expressions in Perl CS/BIO 271 – Introduction to Bioinformatics.
GREP. Whats Grep? Grep is a popular unix program that supports a special programming language for doing regular expressions The grammar in use for software.
May 2008CLINT-LIN Regular Expressions1 Introduction to Computational Linguistics Regular Expressions (Tutorial derived from NLTK)
JavaScript III ECT 270 Robin Burke. Outline Validation examples password more complex Form validation Regular expressions.
Unit 11 –Reglar Expressions Instructor: Brent Presley.
CGS – 4854 Summer 2012 Web Site Construction and Management Instructor: Francisco R. Ortega Chapter 5 Regular Expressions.
Standard Types and Regular Expressions CS 480/680 – Comparative Languages.
Regular Expressions /^Hel{2}o\s*World\n$/ SoftUni Team Technical Trainers Software University
An Introduction to Regular Expressions Specifying a Pattern that a String must meet.
Regular Expressions /^Hel{2}o\s*World\n$/ SoftUni Team Technical Trainers Software University
Pattern Matching: Simple Patterns. Introduction Programmers often need to scan a file, directory, etc. for a specific substring. –Find all files that.
OOP Tirgul 11. What We’ll Be Seeing Today  Regular Expressions Basics  Doing it in Java  Advanced Regular Expressions  Summary 2.
May 2006CLINT-LIN Regular Expressions1 Introduction to Computational Linguistics Regular Expressions (Tutorial derived from NLTK)
REGULAR EXPRESSION Java provides the java.util.regex package for pattern matching with regular expressions. Java regular expressions are very similar.
Regular Expressions Upsorn Praphamontripong CS 1110
/^Hel{2}o\s*World\n$/
Strings, Characters and Regular Expressions
CSC 594 Topics in AI – Natural Language Processing
University of Central Florida COP 3330 Object Oriented Programming
Java Programming Course Regular Expression
/^Hel{2}o\s*World\n$/
CSC 594 Topics in AI – Natural Language Processing
JAVA RegEx Manish Shrivastava 11/11/2018.
Regular Expressions in Java
CSE 1020:Software Development
Selenium WebDriver Web Test Tool Training
Matcher functions boolean find() Attempts to find the next subsequence of the input sequence that matches the pattern. boolean lookingAt() Attempts to.
Regular Expressions in Java
Regular Expressions in Java
Regular Expression in Java 101
Regular Expressions in Java
Presentation transcript:

1 Form Validation

Validation  Validation of form data can be cumbersome using the basic techniques  StringTokenizer  If-else statements  Most of the time you want to validate against a pattern rather than a specific value  E.g. all digits or all letters or letters with one space in between  What pattern would define any web URL? Any address?

Regular Expressions  One technique is to define your pattern using what is called a Regular Expression (RegEx) and process it using a RegEx engine  For example, the regex (\d+) means one or more digits  Java SE and Android have built-in RegEx functionality  java.util.regex

4 A Quick Introduction to Regular Expressions

Regular Expressions  Regular expressions (regex's) are sets of symbols and syntactic elements used to match patterns of text.

Basic Syntax CharUsageExample.Matches any single character.at = cat, bat, rat, 1at… *Matches zero or more occurrences of the single preceding character.*at = everything that ends with at 0*123 = 123, 0123, 00123… […] [^…] Matches any single character of the ones contained Matches any single character except for the ones contained [cbr]at = cat, bat, rat. [^bc]at = rat, sat…, but not bat, cat. ]*> = ^Beginning of line^a = line starts with a $End of line^$ = blank line (starts with the end of line) \Escapes following special character:. \ / & [ ] * + -> \. \\ \/ \& \[ \] \* \+ [cbr]at\. = matches cat., bat. and rat. only

7 Matches  Input string consumed from left to right  Match ranges: inclusive of the beginning index and exclusive of the end index  Example: Current REGEX is: foo Current INPUT is: foofoofoo I found the text "foo" starting at index 0 and ending at index 3. I found the text "foo" starting at index 3 and ending at index 6. I found the text "foo" starting at index 6 and ending at index 9.

Character Classes [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) [a-d[m-p]]a through d, or m through p: [a-dm-p] (union) [a-z&&[def]]d, e, or f (intersection) [a-z&&[^bc]]a through z, except for b and c: [ad-z] (subtraction) [a-z&&[^m-p]]a through z, and not m through p: [a-lq-z] (subtraction)

Predefined Character Classes.Any character (may or may not match line terminators) \dA digit: [0-9] \DA non-digit: [^0-9] \sA whitespace character: [ \t\n\x0B\f\r] \SA non-whitespace character: [^\s] \wA word character: [a-zA-Z_0- 9] \WA non-word character: [^\w]

10 Quantifier Types  There are three types of regex matching styles:  Greedy  Reluctant  Possessive

11 Greedy  Example: Current REGEX is:.*foo Current INPUT is: xfooxxxxxxfoo I found the text "xfooxxxxxxfoo" starting at index 0 and ending at index 13. Greedy: first, the quantified portion of the expression eats the whole input string and tries for a match. If it fails, the matcher backs off the input string by one character and tries again, until a match is found. Greedy is the default style

12 Reluctant  Example: Current REGEX is:.*?foo Current INPUT is: xfooxxxxxxfoo I found the text "xfoo" starting at index 0 and ending at index 4. I found the text "xxxxxxfoo" starting at index 4 and ending at index 13. Reluctant: starts to match at the beginning of the input string. Then, iteratively eats another character until the whole input string is eaten. Note the ? On the expression, this indicates the previous portion should he handled reluctantly

13 Possessive  Example Current REGEX is:.*+foo Current INPUT is: xfooxxxxxxfoo No match found. Possessive: try to match only once on the whole input stream. NOTE the + sign

Examples GreedyReluctantPosse- ssive Meaning X?X??X?+ X, once or not at all X*X*?X*+ X, zero or more times X+X+?X++ X, one or more times X{n} X{n}?X{n}+ X, exactly n times X{n,}X{n,}?X{n,}+ X, at least n times X{n,m}X{n,m}?X{n,m} + X, at least n but not more than m times

Groups  With parentheses, we can create groups to apply quantifiers to several characters: “(abc)+”  Also useful for parsing results (see last slide)  Groups are numbered by counting their opening parentheses from left to right  Example: groups in “((A)(B(C)))” 1. ((A)(B(C))) 2. (A) 3. (B(C)) 4. (C)

Boundary matchers ^The beginning of a line $ The end of a line \b A word boundary \B A non-word boundary \A The beginning of the input \G The end of the previous match \Z The end of the input but for the final terminator, if any \z The end of the input Current REGEX is: \bdog\b Current INPUT is: The doggie plays in the yard. No match found. Current REGEX is: \bdog\b Current INPUT is: The dog plays in the yard. Match found.

Using Regular Expressions in Java

String  The String class contains several regex based methods  split(String regex) – splits a String using the regEx as a delimiter  replaceAll(String regex, String replacement) – replaces all match patterns with replacement  matches(String regex) – returns if the string matches the regex  These methods are sufficient for most simple validation tasks

split() example  NOTE: notice we are splitting against a literal period.  Since the period is part of the regex language, it must be escaped -> \.  But since you need to escape the backslash in Java -> \\. String testString = "This.is.a.test"; String[] data = testString.split("\\."); System.out.println(Arrays.toString(data)); OUTPUT: [This, is, a, test]

replaceAll() example  NOTE: we need to reassign the output string of the method to the variable since String instances are immutable String testString = "This.is.a.test.test.test"; testString = testString.replaceAll("\\.test", ""); System.out.println(testString); OUTPUT: This.is.a

matches() example  Regex description:  Starts with “This”  Has one or more groups of one or more whitespaces followed by one or more word characters  Followed by a single whitespace  Followed by “test” String match = "This is a test"; System.out.println(match.matches("This(\\s+\\w+)+\\stest")); OUTPUT: true

22 Java RegEx API  Two important classes:  java.util.regex.Pattern -- a compiled representation of a regular expression  java.util.regex.Matcher -- an engine that performs match operations by interpreting a Pattern  Example Pattern p = Pattern.compile("a*b"); Matcher m = p.matcher("aaaaab"); boolean b = m.matches();

Pattern  Patterns are compiled regular expressions.  Pattern instances are useful if you need to do a lot of work with the same regular expression  more efficient to compile it once and reuse it.  e.g. for parsing tasks

Pattern  The Pattern class and its companion, Matcher, are also a lot more powerful than the small amount of functionality exposed by StringMatcher  Can be used to find repeating patterns within a string  A Pattern is created using the static Pattern.compile(String regEx) method

Matcher  Once a Pattern is compiled, a Matcher instance can be obtained using matcher(String inputString) method of Pattern  Some common Matcher methods:  matches() – used to determine if the inputString matches the pattern used to create this Matcher  find() – used to loop through all the pattern matches in the inputString  group(int index) – isolates the value held in a specific group of the match  1-indexed, group 0 represents the whole match

Example  Regex description:  Starts with zero or more “a” characters  Followed by “b” Pattern p = Pattern.compile("a*b"); Matcher m = p.matcher("aaaaab"); boolean b = m.matches(); OUTPUT: true

Example import java.util.regex.*; public class RegEx{ public static void main( String args[] ){ String amounts = "$1.57 $ $19.30 $0.30 $0.00 $41.10 $5.1 $.5"; Pattern strMatch = Pattern.compile( "\\$(\\d+)\\.(\\d\\d)" ); Matcher m = strMatch.matcher( amounts ); while ( m.find() ){ System.out.println( "$" + ( Integer.parseInt( m.group(1) ) + 5 ) + "." + m.group(2) ); }  Regex description:  Starts with “$”  Followed by one or more digits  Followed by a period  Followed by two digits OUTPUT: $6.57 $ $24.30 $5.30 $5.00 $46.10

28 Additional References  SUN regexps tutorial  Java.util.regex API for Android file:///C:/android-sdk-windows/docs/reference/java/util/regex/package- summary.html

UI Validation in Android  EditText widgets have a means of limiting what types of characters using the inputType XML attribute  However, this is not enough to handle specific cases such as  does the cellphone number match a Philippine number (+63xxxxxxx) or a Globe/SMART/Sun number

UI Validation in Android  Regex Validation can be done in several places  Check the current text as you are typing  Requires a KeyListener  Can be a bit tricky  At a predefined point (e.g. when moving to the next screen)  Prior to triggering the next activity or closing the current activity extract all the strings and check against the patterns

Things to think about  What are the expressions for the following?  address  Web URL  Cellphone/Phone number  License plate  Money values (with commas and decimal)  Strong passwords