Perl-Compatible Regular Expressions Part 1

Slides:



Advertisements
Similar presentations
Session 3BBK P1 ModuleApril 2010 : [#] Regular Expressions.
Advertisements

Regular Expressions BKF03 Brian Ciccolo. Agenda Definition Uses – within Aspen and beyond Matching Replacing.
FORM VALIDATION Faheem Ahmed Khokhar. FORM VALIDATION Faheem Ahmed Khokhar.
BBK P1 Module2010/11 : [‹#›] Regular Expressions.
Chapter 14 Perl-Compatible Regular Expressions Part 1.
Data Manipulation & Regular Expressions CSCI 215.
IS 1181 IS 118 Introduction to Development Tools Chapter 4 String Manipulation and Regular Expressions.
Scripting Languages Chapter 8 More About Regular Expressions.
UNIX Filters.
Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Verifying Submitted Form Data with JavaScript Instructor:
Regex Wildcards on steroids. Regular Expressions You’ve likely used the wildcard in windows search or coding (*), regular expressions take this to the.
More on Regular Expressions Regular Expressions More character classes \s matches any whitespace character (space, tab, newline etc) \w matches.
REGULAR EXPRESSIONS CHAPTER 14. REGULAR EXPRESSIONS A coded pattern used to search for matching patterns in text strings Commonly used for data validation.
Last Updated March 2006 Slide 1 Regular Expressions.
Regular Expressions Week 07 TCNJ Web 2 Jean Chu. Regular Expressions Regular Expressions are a powerful way to validate and format text strings that may.
PHP Workshop ‹#› Data Manipulation & Regex. PHP Workshop ‹#› What..? Often in PHP we have to get data from files, or maybe through forms from a user.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
PHP Using Strings 1. Replacing substrings (replace certain parts of a document template; ex with client’s name etc) mixed str_replace (mixed $needle,
RegExp. Regular Expression A regular expression is a certain way to describe a pattern of characters. Pattern-matching or keyword search. Regular expressions.
1 DIG 3134 – Lecture 10: Regular Expressions and preg_match in PHP and Validating Inputs Michael Moshell University of Central Florida Internet Software.
Regular Expressions Regular expressions are a language for string patterns. RegEx is integral to many programming languages:  Perl  Python  Javascript.
PHP with Regular Expressions Web Technologies Computing Science Thompson Rivers University.
REGEX. Problems Have big text file, want to extract data – Phone numbers (503)
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.
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,
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.
PHP’s Regular Expression Functions (Perl Compatible) Examples taken from: Beginning PHP 5 and MySQL 5 From Novice to Professional.
1 DIG 3563: Lecture 2a: Regular Expressions Michael Moshell University of Central Florida Information Management.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Regular Expressions.
CompSci 101 Introduction to Computer Science November 18, 2014 Prof. Rodger.
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.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
8 1 String Manipulation CGI/Perl Programming By Diane Zak.
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.
Regular Expressions.
PROGRAMMING THE BASH SHELL PART III by İlker Korkmaz and Kaya Oğuz
Regular Expressions Copyright Doug Maxwell (
Finding the needle(s) in the textual haystack
Regular Expressions Upsorn Praphamontripong CS 1110
Strings and Serialization
Lecture 19 Strings and Regular Expressions
Advanced Regular Expressions
Finding the needle(s) in the textual haystack
PHP loeng 2.
LING/C SC/PSYC 438/538 Lecture 8 Sandiway Fong.
Finding the needle(s) in the textual haystack
Chapter 4 Procedural Methods.
Chapter 12: Text Processing
Chapter 12: Text Processing
Advanced String handling
The Linux Command Line Chapter 27
LING/C SC/PSYC 438/538 Lecture 10 Sandiway Fong.
10.1 Character Testing.
CSCI 431 Programming Languages Fall 2003
CS 1111 Introduction to Programming Fall 2018
Chapter 7 Procedural Methods.
Data Manipulation & Regex
Matcher functions boolean find() Attempts to find the next subsequence of the input sequence that matches the pattern. boolean lookingAt() Attempts to.
CSE 303 Concepts and Tools for Software Development
CIT 383: Administrative Scripting
- Regular expressions:
Validation using Regular Expressions
Regular Expression: Pattern Matching
Boolean in C++ CSCE 121.
REGEX.
Perl Regular Expressions – Part 1
Regular Expressions.
The Linux Command Line Chapter 27
LING/C SC/PSYC 438/538 Lecture 12 Sandiway Fong.
Presentation transcript:

Perl-Compatible Regular Expressions Part 1 Chapter 14 Perl-Compatible Regular Expressions Part 1

preg_match The match function is 1st argument is the pattern int preg_match(string $pattern, string $str ) 1st argument is the pattern 2nd argument is the subject string Returns number of matches found: 1 (true) if there is a match – it stops at the first match found! 0 (false) otherwise

pcre.php Script 14.1 on page 435 http://csweb.hh.nku.edu/csc301/frank/ch14/pcre.php ch14\pcre.php

Character Classes [A-Z] uppercase letter [a-z] lowercase letter [0-9] digit {5} exactly five {3,} three or more {1,4} one to four

Metacharacters ^ beginning of string $ end of string | or

Regular Expression Username – 5 to 10 lowercase letters? $pattern = '/^[a-z]{5,10}$/'; if (!preg_match($pattern, $username)) { echo "<p>$username is invalid.</p>"; }

Regular Expressions Social Security Number 123-45-6789 $pattern = '/^[0-9]{3}-[0-9]{2}-[0-9]{4}$/'; if (!preg_match($pattern, $ssn)) { echo "<p>$ssn is invalid.</p>"; }

Character classes \d any digit [0-9] \D any non-digit [^0-9] \w Any letter, number, or underscore [A-Za-z0-9_] \W Anything that is not a letter, number, or underscore [^A-Za-z0-9_]

Character classes \s whitespace (space, tab, new line, etc.) [\f\r\t\n\v] \S Any non-whitespace [^\f\r\t\n\v]

Regular Expressions Social Security Number 123-45-6789 $pattern = '/^\d{3}-\d{2}-\d{4}$/'; if (!preg_match($pattern, $ssn)) { echo "<p>$ssn is invalid.</p>"; }

Metacharacters [0-9]* * = zero or more [A-Z]+ + = one or more . . = match any character ([A-Z]\. )? ? = either zero or one

Regular Expressions Last name: Frank $pattern = '/^[A-Z][a-z]+$/'; if (!preg_match($pattern, $last)) { echo "<p>$last is invalid.</p>"; }

Regular Expressions Name: Charles Frank or Charles E. Frank $pattern = '/^[A-Z][a-z]+ ([A-Z]\. )?[A-Z][a-z]+ $/'; if (!preg_match($pattern, $name)) { echo "<p>$name is invalid.</p>"; }

Zip Code Page 444 /^\d{5}(-\d{4})?$/

Email Address Page 445 /^[\w.]+@[\w.-]+\.[A-Za-z]{2,6}$/