REGULAR EXPRESSIONS CHAPTER 14. REGULAR EXPRESSIONS A coded pattern used to search for matching patterns in text strings Commonly used for data validation.

Slides:



Advertisements
Similar presentations
Regular Expressions using Ruby Assignment: Midterm Class: CPSC5135U – Programming Languages Teacher: Dr. Woolbright Student: James Bowman.
Advertisements

Regular Expressions in Perl By Josue Vazquez. What are Regular Expressions? A template that either matches or doesn’t match a given string. Often called.
Regular Expressions (in Python). Python or Egrep We will use Python. In some scripting languages you can call the command “grep” or “egrep” egrep pattern.
Chapter 14 Perl-Compatible Regular Expressions Part 1.
Regular Expression Original Notes by Song Guo. What Regular Expressions Are Exactly - Terminology a regular expression is a pattern describing a certain.
ISBN Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl –(on reserve.
1 CSE 390a Lecture 7 Regular expressions, egrep, and sed slides created by Marty Stepp, modified by Jessica Miller and Ruth Anderson
1 CSE 390a Lecture 7 Regular expressions, egrep, and sed slides created by Marty Stepp, modified by Jessica Miller
LING 388: Language and Computers Sandiway Fong Lecture 2: 8/23.
W3101: Programming Languages (Perl) 1 Perl Regular Expressions Syntax for purpose of slides –Regular expression = /pattern/ –Broader syntax: if (/pattern/)
Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl Linux editors and commands (e.g.
Scripting Languages Chapter 8 More About Regular Expressions.
Form Validation CS What is form validation?  validation: ensuring that form's values are correct  some types of validation:  preventing blank.
Regular Expressions in ColdFusion Applications Dave Fauth DOMAIN technologies Knowledge Engineering : Systems Integration : Web.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Regular Expressions A regular expression defines a pattern of characters to be found in a string Regular expressions are made up of – Literal characters.
Last Updated March 2006 Slide 1 Regular Expressions.
System Programming Regular Expressions Regular Expressions
 Text Manipulation and Data Collection. General Programming Practice Find a string within a text Find a string ‘man’ from a ‘A successful man’
Modern JavaScript Develop And Design Instructor’s Notes Chapter 10 – Working with Forms Modern JavaScript Design And Develop Copyright © 2012 by Larry.
PHP Using Strings 1. Replacing substrings (replace certain parts of a document template; ex with client’s name etc) mixed str_replace (mixed $needle,
Regular Expressions in Perl Part I Alan Gold. Basic syntax =~ is the matching operator !~ is the negated matching operator // are the default delimiters.
REGULAR EXPRESSIONS. Lexical Analysis Lexical analysers can be constructed by programs such as LEX These programs employ as input a description of the.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
RegExp. Regular Expression A regular expression is a certain way to describe a pattern of characters. Pattern-matching or keyword search. Regular expressions.
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,
Python Regular Expressions Easy text processing. Regular Expression  A way of identifying certain String patterns  Formally, a RE is:  a letter or.
COMP313A Programming Languages Lexical Analysis. Lecture Outline Lexical Analysis The language of Lexical Analysis Regular Expressions.
1 CSC 594 Topics in AI – Text Mining and Analytics Fall 2015/16 4. Document Search and Regular Expressions.
Regular Expressions.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.
PHP with Regular Expressions Web Technologies Computing Science Thompson Rivers University.
Regular Expressions in PHP. Supported RE’s The most important set of regex functions start with preg. These functions are a PHP wrapper around the PCRE.
REGEX. Problems Have big text file, want to extract data – Phone numbers (503)
Corpus Linguistics- Practical utilities (Lecture 7) Albert Gatt.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
Regular Expressions for PHP Adding magic to your programming. Geoffrey Dunn
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming regular expressions.
Regular Expressions in Perl CS/BIO 271 – Introduction to Bioinformatics.
Regular Expressions What is this line all about? while (!($search =~ /^\s*$/)) { It’s a string search just like before, but with a huge twist – regular.
Appendix A: Regular Expressions It’s All Greek to Me.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
12. Regular Expressions. 2 Motto: I don't play accurately-any one can play accurately- but I play with wonderful expression. As far as the piano is concerned,
CS346 Regular Expressions1 Pattern Matching Regular Expression.
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.
Introduction to sed. Sed : a “S tream ED itor ” What is Sed ?  A “non-interactive” text editor that is called from the unix command line.  Input text.
PHP’s Regular Expression Functions (Perl Compatible) Examples taken from: Beginning PHP 5 and MySQL 5 From Novice to Professional.
CSC 2720 Building Web Applications PHP PERL-Compatible Regular Expressions.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
Regular Expressions CS 2204 Class meeting 6 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Regular Expressions.
1 Validating user input is the bane of every software developer’s existence. When you are developing cross-browser web applications (IE4+ and NS4+) this.
Java Script Pattern Matching Using Regular Expressions.
Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.
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.
INT222 – Internet Fundamentals Week 11: RegExp Object and HTML5 Form Validation 1.
An Introduction to Regular Expressions Specifying a Pattern that a String must meet.
Chapter 4 © 2009 by Addison Wesley Longman, Inc Pattern Matching - JavaScript provides two ways to do pattern matching: 1. Using RegExp objects.
Variable Variables A variable variable has as its value the name of another variable without $ prefix E.g., if we have $addr, might have a statement $tmp.
Pattern Matching: Simple Patterns. Introduction Programmers often need to scan a file, directory, etc. for a specific substring. –Find all files that.
Lesson 4 String Manipulation. Lesson 4 In many applications you will need to do some kind of manipulation or parsing of strings, whether you are Attempting.
Perl-Compatible Regular Expressions Part 1
Chapter 19 PHP Part II Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
Regular Expressions and Grep
ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS
Presentation transcript:

REGULAR EXPRESSIONS CHAPTER 14

REGULAR EXPRESSIONS A coded pattern used to search for matching patterns in text strings Commonly used for data validation Patterns are strings inside single quotation marks and delimiters, usually /

REGULAR EXPRESSIONS Example pattern: $pattern = '/NC/'; The pattern is then sent with the string to a built-in PHP function.

REGULAR EXPRESSIONS preg_match(pattern, subject); returns 1 if the pattern is found, 0 if not, FALSE if there is an error in the pattern stops after the first match preg_match_all(); will find all matches

REGULAR EXPRESSIONS Create a regular expression: $pattern = '/Larry/'; Two strings to test: $author = 'Larry Ullman'; $editor = 'Rebecca Gulick'; Search for the pattern: $author_match = preg_match($pattern, $author); // $author_match is 1 $author_match = preg_match($pattern, $editor); // $author_match is 0

REGULAR EXPRESSIONS if ($author_match === false) { echo 'Error testing author name.'; } else if ($author_match === 0) { echo 'Author name does not contain Larry.'; } else { echo 'Author name contains Larry.'; }

DEFINING PATTERNS In addition to the literal characters being matched, meta-characters have other meanings: CharacterMeaning \Escape ^Beginning of a string $End of a string.Any single character except for newline |Or [ ]Beginning/ending of a class ( )Beginning/ending of a sub-pattern { }Beginning/ending of a quantifier

EXAMPLES Single character. $pattern = '/1.99/'; matches: 1.99, 1B99 or 1299 To match a period in a string, it must be escaped: $pattern = '/1\.99/'; matches: 1.99 only

EXAMPLES Specifying where characters are found: ^ at the beginning $ at the end $pattern = '/^a/'; matches any string beginning with the letter a $pattern = '/a$/'; matches any string ending with the letter a

EXAMPLES Pipe operator: | => or yes|no Grouping subpatterns: () (even|heavy) handed col(o|ou)r

CASE SENSITIVITY By default, a search is case sensitive. To perform a case-insensitive search, include a lowercase i after the closing / : $pattern = '/cat/i';

QUANTIFIERS CharacterMeaning ? 0 or 1 *0 or more +1 or more {x}Exactly x occurrences {x,y}Between x and y occurrences (inclusive) {x,}At least x occurrences Apply to the preceding character/group a{3} matches aaa a? matches 0 or 1 a's (ab)? matches 0 or 1 ab's

EXAMPLE Pattern for a five-digit number: $pattern = '/(0|1|2|3|4|5|6|7|8|9){5}/'; This will match but also will match contained within any other string. Use ^ and $ to match exactly: $pattern = '/^(0|1|2|3|4|5|6|7|8|9){5}$/';

CHARACTER CLASSES Square brackets define a set with an implied | or – for a range: [aeiou] matches a vowel (the | is implied) [a-z] matches any single lowercase letter [0-9] matches any single digit [A-Za-z] matches any character in the alphabet

CHARACTER CLASSES EXAMPLE A 3-letter, lowercase word: [a-z]{3} 5-digit zip code: ^[0-9]{5}$

CHARACTER CLASSES EXAMPLE $string = 'The product code is MBT-3461.'; preg_match('/MB[TF]/', $string); // Matches MBT and returns 1 preg_match('/[.]/', $string); // Matches. and returns 1 // Equivalent to preg_match('/\./', $string); preg_match('/[13579]/', $string); // Matches 3 and returns 1

METACHARACTERS characters which have special meanings in patterns such as / \. [ ] $ ^ ( ) – most metacharacters lose their special meanings inside a character class the exceptions: ^ (caret) negation: match any character except those in the class - (dash): represents a range of characters between the ones on either side to find a dash as part of a character class, the dash must be at the end.

METACHARACTERS Usage: $string = 'The product code is MBT-3461.'; preg_match('/MB[^FT]/', $string); // Matches nothing and returns 0 preg_match('/MBT[^^]/', $string); // Matches MBT- and returns 1 preg_match('/MBT-[1-5]/', $string); // Matches MBT-3 and returns 1 preg_match('/MBT[_*-]/', $string); // Matches MBT- and returns 1

CHARACTER CLASS SHORTCUTS ClassShortcutMeaning [0-9]\dAny digit [\f\r\t\n\v]\sAny white space [A-Za-z0-9]\wAny alphanumeric character [^0-9]\DNot a digit [^\f\r\t\n\v]\SNot white space [^A-Za-z0-9]\WNot an alphanumeric character

MORE DETAILED RESULTS Recall that the default for preg_match() is to stop and return 1 when the first occurrence of the pattern is found. To find out what, exactly, was found that matched the pattern include an optional 3 rd parameter: preg_match($pattern, $subject, $matches);

FINDING ALL MATCHES To find out how many matches were found, use preg_match_all() and include an optional 3 rd parameter: preg_match_all($pattern, $subject, $matches); This will return an integer or FALSE if there is an error in the pattern.

FINDING ALL MATCHES Additionally, preg_match_all($pattern, $subject, $matches); will put all of the found results into a multidimensional array assigned to $matches

USING MODIFIERS Pattern modifiers are parts of regular expressions, but these are placed after the ending delimiter. CharacterResult AAnchors the pattern to the beginning of the string IEnables case-insensitive mode MEnables multi-line matching SHas the period match every character including newline XIgnores most white space UPerforms as non-greedy match.

MATCHING AND REPLACING PATTERNS preg_replace($pattern, $replacement, $subject,$num) This function returns the altered (or unaltered if not found) $subject. The optional fourth parameter limits the number of replacements made.