Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Regular Expression Original Notes by Song Guo. What Regular Expressions Are Exactly - Terminology a regular expression is a pattern describing a certain.
W3101: Programming Languages (Perl) 1 Perl Regular Expressions Syntax for purpose of slides –Regular expression = /pattern/ –Broader syntax: if (/pattern/)
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved.
CS 330 Programming Languages 10 / 10 / 2006 Instructor: Michael Eckmann.
Introduction to PHP and MySQL Kirkwood Center for Continuing Education By Fred McClurg, © Copyright 2010, All Rights Reserved.
Using regular expressions Search for a single occurrence of a specific string. Search for all occurrences of a string. Approximate string matching.
Regular Expressions. What are regular expressions? A means of searching, matching, and replacing substrings within strings. Very powerful (Potentially)
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
Introduction to PHP and MySQL Kirkwood Center for Continuing Education By Fred McClurg, © Copyright 2014, Fred McClurg, All Rights.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
Validating user input Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
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.
slides created by Marty Stepp
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.
Lecture 7: Perl pattern handling features. Pattern Matching Recall =~ is the pattern matching operator A first simple match example print “An methionine.
Language Recognizer Connecting Type 3 languages and Finite State Automata Copyright © – Curt Hill.
Programming Perl in UNIX Course Number : CIT 370 Week 4 Prof. Daniel Chen.
 Text Manipulation and Data Collection. General Programming Practice Find a string within a text Find a string ‘man’ from a ‘A successful man’
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
Sys.Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 18: Regular Expressions in PHP.
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 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,
1 CSC 594 Topics in AI – Text Mining and Analytics Fall 2015/16 4. Document Search and Regular Expressions.
Regular Expression in Java 101 COMP204 Source: Sun tutorial, …
Regular Expression Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
PHP with Regular Expressions Web Technologies Computing Science Thompson Rivers University.
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 in PHP. Supported RE’s The most important set of regex functions start with preg. These functions are a PHP wrapper around the PCRE.
CPTG286K Programming - Perl Chapter 7: Regular Expressions.
Overview A regular expression defines a search pattern for strings. Regular expressions can be used to search, edit and manipulate text. The pattern defined.
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. Overview Regular expressions allow you to do complex searches within text documents. Examples: Search 8-K filings for restatements.
Regular Expressions for PHP Adding magic to your programming. Geoffrey Dunn
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.
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.
May 2008CLINT-LIN Regular Expressions1 Introduction to Computational Linguistics Regular Expressions (Tutorial derived from NLTK)
Pattern Matching II. Greedy Matching When dealing with quantifiers, Perl’s pattern matcher is by default greedy. For example, –$_ = “Bob sat next to the.
CSC 2720 Building Web Applications PHP PERL-Compatible Regular Expressions.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Regular Expressions.
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.
7 Copyright © 2009, Oracle. All rights reserved. Regular Expression Support.
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.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2016, Fred McClurg, All Rights.
May 2006CLINT-LIN Regular Expressions1 Introduction to Computational Linguistics Regular Expressions (Tutorial derived from NLTK)
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.
COMP234-Perl Variables, Literals Context, Operators Command Line Input Regex Program template.
RE Tutorial.
Looking for Patterns - Finding them with Regular Expressions
CSC 594 Topics in AI – Natural Language Processing
Regular Expressions in Perl
Regular Expressions and perl
Lecture 9 Shell Programming – Command substitution
CSC 594 Topics in AI – Natural Language Processing
Selenium WebDriver Web Test Tool Training
CIT 383: Administrative Scripting
Presentation transcript:

Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights Reserved.

2 Regular Expressions /php/slides/chapter17b.regex.ppt

Defined: A method of specifying a search string using a number of special characters that can precisely match a substring. Purpose: Regular expressions allow you to perform complex pattern matching on strings. A small regular expression can replace the need for a large amount of code Example for validation: 3 Regular Expressions

Description: Quantifiers are wildcards that specify how many times to match the preceding string pattern. 4 Regular Expression Quantifiers QuantifierMeaning Alternate Notation * Match zero or more of previous {0,} + Match one or more of previous {1,} ? Match one or zero of previous {0,1} {num} Match exactly num of previous {min,} Match at least min of previous {min,max} Match at least min but not more than max of previous characters

Description: Anchors define a specific location for the match to take place. 5 Regular Expression Anchors AnchorMeaning ^ Match start of line $ Match end of line. Match any single character \b Match word boundary \B Match non-word boundary

Defined: A method of matching a set of characters. Examples Match any upper or lower case alpha character: [A-Za-z] Match any lower case vowel character: [aeiou] Match any numeric digit character: [0-9] 6 Character Classes

Defined: A method of matching characters that are outside the specified set. Examples: Match any non-alpha character: [^A-Za-z] Match any non-vowel character: [^aeiou] Match any non-numeric digit: [^0-9] Match any character outside the ASCII range (e.g. non- printable characters like tab, linefeed, CTRL, etc.): [^ -~] 7 Negated Character Classes

Defined: The vertical pipe character allows you to match one string or another. Example: Match the string “red”, “white”, or “blue”: Regular Expression: red|white|blue 8 Alternatives

Defined: Parentheses can be used to group regular expressions. Example: Match any of the following: a long time ago a long long time ago a long long long time ago Regular Expression: (long )+ 9 Subpatterns

Greedy Defined: By default, regular expressions are “greedy” meaning that '.* ' and '.+ ' will always match as the longest string. Example: Given the string: do not press And the regular expression: The resulting match would be: not 10 Greedy Quantifiers

Non-greedy Defined: Non-greedy quantifiers specify minimal string matching. A question mark “ ? ” placed after the wildcard, cancels the “greedy” regular expression behavior. Example: Given the string: do not press And the regular expression: The resulting match would be: 11 Non-greedy Quantifiers

Defined: A method of matching a set of characters. 12 Perl Style Character Classes SymbolMeaning Alternate Notation \s Match whitespace [\r\n \t] \S Match non-whitespace [^\r\n \t] \w Match word character [A-Za-z0-9_] \W Match non-word character [^A-Za-z0-9_] \d Match a numeric digit [0-9] \D Match a non-numeric digit [^0-9]

Example match upper and lower case “abc”: '/abc/i' 13 Regular Expression Modifiers ModifierMeaning i Ignore case (case insensitive) s Dot matches all characters m Match start and end of line anchors at embedded new lines in search string Description: Modifiers change the matching behavior of the regular expression.

Discussion: PHP functions that use Perl regular expressions start with “preg _ ”. The regular expressions are in Perl format meaning, they start and end with a slash. In order to prevent variable expansion, regular expressions are usually specified with single quotes. 14 Regular Expressions in PHP

Discussion: The function “preg_match()” returns the number of times a pattern matches a string. Because it returns zero if there are no matches, this function works ideal in an “if” statement. Example:<?php $subject = "Jack and Jill went up the hill."; $subject = "Jack and Jill went up the hill."; $pattern = '/jack|jill/i'; $pattern = '/jack|jill/i'; if ( preg_match( $pattern, $subject ) ) if ( preg_match( $pattern, $subject ) ) { printf( "Regular expression matched." ); printf( "Regular expression matched." ); }?> 15 Using “preg_match()”

Discussion: The function “preg_match_all()” returns the number of times a pattern matches a string. Because it returns zero if there are no matches, this function works ideal in an “if” statement. Example:<?php $subject = "Every good boy deserves fudge."; $subject = "Every good boy deserves fudge."; $pattern = '/[aeiou]/i'; $pattern = '/[aeiou]/i'; preg_match_all( $pattern, $subject, $matches ); preg_match_all( $pattern, $subject, $matches ); $total = count( $matches[0] ); // matches array $total = count( $matches[0] ); // matches array printf( "String contains %d vowels", printf( "String contains %d vowels", $total ); $total );?> 16 Using “preg_match_all()”

to be continued... rses/php/slides/recipe01.ppt rses/php/slides/recipe01.ppt 17