SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Regular Expressions
PHP Programming
2
PHP Programming with MySQL, 2nd
Edition
Regular Expressions
Regular Expressions are patterns that are
used for matching and manipulating strings
according to specified rules
PHP supports two types of regular
expressions:
POSIX Extended
Perl Compatible Regular Expressions
3
PHP Programming with MySQL, 2nd
Edition
Regular Expressions
4
PHP Programming with MySQL, 2nd
Edition
Regular Expressions
Pass to the preg_match() the regular
expression pattern as the first argument and
a string containing the text you want to
search as the second argument
preg_match(pattern, string);
5
PHP Programming with MySQL, 2nd
Edition
Writing Regular Expression Patterns
A regular expression pattern is a special text string that
describes a search pattern
Regular expression patterns consist of literal characters
and metacharacters, which are special characters that
define the pattern-matching rules
Regular expression patterns are enclosed in delimiters
The most common delimiter is the forward slash (/)
6
PHP Programming with MySQL, 2nd
Edition
Regular Expression Patterns
7
PHP Programming with MySQL, 2nd
Edition
Matching Any Character
A period (.) in a regular expression pattern
specifies that the pattern must contain a value
at the location of the period
A return value of 0 indicates that the string
does not match the pattern and 1 if it does
$ZIP = "015";
preg_match("/...../", $ZIP); // returns 0
$ZIP = "01562";
preg_match("/...../", $ZIP); // returns 1
8
PHP Programming with MySQL, 2nd
Edition
Matching Characters
An anchor specifies that the pattern must appear at a
particular position in a string
The ^ metacharacter anchors characters to the beginning
of a string
The $ metacharacter anchors characters to the end of a
string
$URL = "http://www.dongosselin.com";
preg_match("/^http/", $URL); // returns 1
9
PHP Programming with MySQL, 2nd
Edition
To specify an anchor at the beginning of a string, the
pattern must begin with a ^ metcharacter
$URL = "http://www.dongosselin.com";
eregi("^http", $URL); // returns 1;
To specify an anchor at the end of a line, the pattern
must end with the $ metacharacter
$Identifier = "http://www.dongosselin.com";
eregi("com$", $Identifier); // returns 1
Matching Characters
10
PHP Programming with MySQL, 2nd
Edition
Matching Special Characters
To match any metacharacters as literal
values in a regular expression, escape the
character with a backslash (in the following
example, the last four characters in the string must be
‘.com’)
$Identifier = http://www.dongosselin.com";
preg_match("/gov$/", $Identifier);//returns 0
11
PHP Programming with MySQL, 2nd
Edition
Specifying Quantity
Meta characters that specify the quantity of a match
are called quantifiers
12
PHP Programming with MySQL, 2nd
Edition
Specifying Quantity
A question mark (?) quantifier specifies that
the preceding character in the pattern is
optional
(in the following example, the string must begin with ‘http’
or ‘https’)
$URL = "http://www.dongosselin.com";
preg_match("/^https?/", $URL); // returns 1
13
PHP Programming with MySQL, 2nd
Edition
Specifying Quantity
• The addition(+) quantifier specifies that one
or more sequential occurrences of the
preceding characters match
(in the following example, the string must have at least one
character)
$Name = "Don";
preg_match("/.+/", $Name); // returns 1
14
PHP Programming with MySQL, 2nd
Edition
Specifying Quantity
• A asterisk (*) quantifier specifies that zero
or more sequential occurrences of the
preceding characters match
(in the following example, the string must begin with one or
more leading zeros)
NumberString = "00125";
preg_match("/^0*/", $NumberString);//returns 1
15
PHP Programming with MySQL, 2nd
Edition
Specifying Quantity
The { } quantifiers specify the number of times that a character
must repeat sequentially
(in the following example, the string must contain at least five
characters)
preg_match("/ZIP: .{5}$/", " ZIP: 01562");
// returns 1
The { } quantifiers can also specify the quantity as a range
(in the following example, the string must contain between five
and ten characters)
preg_match("/(ZIP: .{5,10})$/", "ZIP:
01562-2607");// returns 1
16
PHP Programming with MySQL, 2nd
Edition
Specifying Subexpressions
A subexpression or subpattern are a set
of characters enclosed in parentheses are
treated as a group
In the example below, the 1 and the area code are optional,
but if included must be in the following format:
1 (707) 555-1234
preg_match("/^(1 )?((.{3}) )?(.{3})(.{4})$/
17
PHP Programming with MySQL, 2nd
Edition
Defining Character Classes
Character classes in regular expressions treat
multiple characters as a single item.
Characters enclosed with the ([]) metacharacters
represent alternate characters that are allowed in
a pattern match
preg_match("/analy[sz]e/", "analyse");//returns 1
preg_match("/analy[sz]e/", "analyze");//returns 1
preg_match("/analy[sz]e/", "analyce");//returns 0
18
PHP Programming with MySQL, 2nd
Edition
Defining Character Classes
The hyphen metacharacter (-) specifies a
range of values in a character class.
The following example ensures that A, B, C, D, or F are the
only values assigned to the $LetterGrade variable:
$LetterGrade = "B";
echo ereg("[A-DF]", $LetterGrade); //
returns true
19
PHP Programming with MySQL, 2nd
Edition
Defining Character Classes
(continued)
The ^ metacharacter (placed immediately
after the opening bracket of a character
class) specifies optional characters to
exclude in a pattern match.
The following example excludes the letter E and G-Z from
an acceptable pattern match in the $LetterGrade variable:
$LetterGrade = "A";
echo ereg("[^EG-Z]", $LetterGrade); //
returns true
20
PHP Programming with MySQL, 2nd
Edition
Defining
Character
Classes
21
PHP Programming with MySQL, 2nd
Edition
Matching Multiple Pattern Choices
The | metacharacter is used to specify an
alternate set of patterns
The | metacharacter is essentially the same as
using the OR operator to perform multiple
evaluations in a conditional expression
22
PHP Programming with MySQL, 2nd
Edition
Pattern Modifiers
Pattern modifiers are letters placed after the
closing delimiter that change the default rules
for interpreting matches
– The pattern modifier, i, indicates that the case
of the letter does not matter when searching
– The pattern modifier, m, allows searches across
newline characters
– The pattern modifier, s, changes how the .
(period) metacharacter works

Weitere ähnliche Inhalte

Was ist angesagt?

Regular expressions in oracle
Regular expressions in oracleRegular expressions in oracle
Regular expressions in oracleLogan Palanisamy
 
4. SQL in DBMS
4. SQL in DBMS4. SQL in DBMS
4. SQL in DBMSkoolkampus
 
7 relational database design algorithms and further dependencies
7 relational database design algorithms and further dependencies7 relational database design algorithms and further dependencies
7 relational database design algorithms and further dependenciesKumar
 
A Role of Lexical Analyzer
A Role of Lexical AnalyzerA Role of Lexical Analyzer
A Role of Lexical AnalyzerArchana Gopinath
 
C++ Langauage Training in Ambala ! BATRA COMPUTER CENTRE
C++  Langauage Training in Ambala ! BATRA COMPUTER CENTREC++  Langauage Training in Ambala ! BATRA COMPUTER CENTRE
C++ Langauage Training in Ambala ! BATRA COMPUTER CENTREjatin batra
 
Normalization
NormalizationNormalization
NormalizationavniS
 
Iipm chapter 1
Iipm chapter 1Iipm chapter 1
Iipm chapter 1iipmff2
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLPkartikaVashisht
 
Functional dependencies and normalization
Functional dependencies and normalizationFunctional dependencies and normalization
Functional dependencies and normalizationdaxesh chauhan
 

Was ist angesagt? (20)

Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
5 introduction-to-c
5 introduction-to-c5 introduction-to-c
5 introduction-to-c
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Pcd question bank
Pcd question bank Pcd question bank
Pcd question bank
 
Regular expressions in oracle
Regular expressions in oracleRegular expressions in oracle
Regular expressions in oracle
 
Perl slid
Perl slidPerl slid
Perl slid
 
4. SQL in DBMS
4. SQL in DBMS4. SQL in DBMS
4. SQL in DBMS
 
C# slid
C# slidC# slid
C# slid
 
7 relational database design algorithms and further dependencies
7 relational database design algorithms and further dependencies7 relational database design algorithms and further dependencies
7 relational database design algorithms and further dependencies
 
Compiler lec 8
Compiler lec 8Compiler lec 8
Compiler lec 8
 
A Role of Lexical Analyzer
A Role of Lexical AnalyzerA Role of Lexical Analyzer
A Role of Lexical Analyzer
 
Database management system session 5
Database management system session 5Database management system session 5
Database management system session 5
 
C++ Langauage Training in Ambala ! BATRA COMPUTER CENTRE
C++  Langauage Training in Ambala ! BATRA COMPUTER CENTREC++  Langauage Training in Ambala ! BATRA COMPUTER CENTRE
C++ Langauage Training in Ambala ! BATRA COMPUTER CENTRE
 
Normalization
NormalizationNormalization
Normalization
 
Iipm chapter 1
Iipm chapter 1Iipm chapter 1
Iipm chapter 1
 
Lexical analysis
Lexical analysisLexical analysis
Lexical analysis
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
 
2.regular expressions
2.regular expressions2.regular expressions
2.regular expressions
 
Functional dependencies and normalization
Functional dependencies and normalizationFunctional dependencies and normalization
Functional dependencies and normalization
 

Andere mochten auch

Iterators, ArrayAccess & Countable (Oh My!) - Madison PHP 2014
Iterators, ArrayAccess & Countable (Oh My!) - Madison PHP 2014Iterators, ArrayAccess & Countable (Oh My!) - Madison PHP 2014
Iterators, ArrayAccess & Countable (Oh My!) - Madison PHP 2014Sandy Smith
 
Architecting with Queues for Scale, Speed, and Separation (DCPHP 3/11/15)
Architecting with Queues for Scale, Speed, and Separation (DCPHP 3/11/15)Architecting with Queues for Scale, Speed, and Separation (DCPHP 3/11/15)
Architecting with Queues for Scale, Speed, and Separation (DCPHP 3/11/15)Sandy Smith
 
GAIQ - Regular expressions-google-analytics
GAIQ - Regular expressions-google-analyticsGAIQ - Regular expressions-google-analytics
GAIQ - Regular expressions-google-analyticsAnkita Kishore
 
Unicode Regular Expressions
Unicode Regular ExpressionsUnicode Regular Expressions
Unicode Regular ExpressionsNova Patch
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsJames Gray
 
Don't Fear the Regex LSP15
Don't Fear the Regex LSP15Don't Fear the Regex LSP15
Don't Fear the Regex LSP15Sandy Smith
 
Multibyte string handling in PHP
Multibyte string handling in PHPMultibyte string handling in PHP
Multibyte string handling in PHPDaniel_Rhodes
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsdavidfstr
 
Lessons from a Dying CMS
Lessons from a Dying CMSLessons from a Dying CMS
Lessons from a Dying CMSSandy Smith
 
TDA Center Depok update 2014 (Concept)
TDA Center Depok update 2014 (Concept)TDA Center Depok update 2014 (Concept)
TDA Center Depok update 2014 (Concept)Herri Setiawan
 
Hyperlocalisation or "localising everything"
Hyperlocalisation or "localising everything"Hyperlocalisation or "localising everything"
Hyperlocalisation or "localising everything"Daniel_Rhodes
 
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?daoswald
 
Architecting with Queues - Northeast PHP 2015
Architecting with Queues - Northeast PHP 2015Architecting with Queues - Northeast PHP 2015
Architecting with Queues - Northeast PHP 2015Sandy Smith
 
How to report a bug
How to report a bugHow to report a bug
How to report a bugSandy Smith
 
Working with Databases and MySQL
Working with Databases and MySQLWorking with Databases and MySQL
Working with Databases and MySQLNicole Ryan
 
Don't Fear the Regex - Northeast PHP 2015
Don't Fear the Regex - Northeast PHP 2015Don't Fear the Regex - Northeast PHP 2015
Don't Fear the Regex - Northeast PHP 2015Sandy Smith
 
Learning Regular Expressions for the Extraction of Product Attributes from E-...
Learning Regular Expressions for the Extraction of Product Attributes from E-...Learning Regular Expressions for the Extraction of Product Attributes from E-...
Learning Regular Expressions for the Extraction of Product Attributes from E-...Volha Bryl
 

Andere mochten auch (20)

Iterators, ArrayAccess & Countable (Oh My!) - Madison PHP 2014
Iterators, ArrayAccess & Countable (Oh My!) - Madison PHP 2014Iterators, ArrayAccess & Countable (Oh My!) - Madison PHP 2014
Iterators, ArrayAccess & Countable (Oh My!) - Madison PHP 2014
 
Architecting with Queues for Scale, Speed, and Separation (DCPHP 3/11/15)
Architecting with Queues for Scale, Speed, and Separation (DCPHP 3/11/15)Architecting with Queues for Scale, Speed, and Separation (DCPHP 3/11/15)
Architecting with Queues for Scale, Speed, and Separation (DCPHP 3/11/15)
 
GAIQ - Regular expressions-google-analytics
GAIQ - Regular expressions-google-analyticsGAIQ - Regular expressions-google-analytics
GAIQ - Regular expressions-google-analytics
 
Unicode Regular Expressions
Unicode Regular ExpressionsUnicode Regular Expressions
Unicode Regular Expressions
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Don't Fear the Regex LSP15
Don't Fear the Regex LSP15Don't Fear the Regex LSP15
Don't Fear the Regex LSP15
 
Multibyte string handling in PHP
Multibyte string handling in PHPMultibyte string handling in PHP
Multibyte string handling in PHP
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Lessons from a Dying CMS
Lessons from a Dying CMSLessons from a Dying CMS
Lessons from a Dying CMS
 
Dom
DomDom
Dom
 
TDA Center Depok update 2014 (Concept)
TDA Center Depok update 2014 (Concept)TDA Center Depok update 2014 (Concept)
TDA Center Depok update 2014 (Concept)
 
Hyperlocalisation or "localising everything"
Hyperlocalisation or "localising everything"Hyperlocalisation or "localising everything"
Hyperlocalisation or "localising everything"
 
Intoduction to php strings
Intoduction to php  stringsIntoduction to php  strings
Intoduction to php strings
 
Grokking regex
Grokking regexGrokking regex
Grokking regex
 
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
 
Architecting with Queues - Northeast PHP 2015
Architecting with Queues - Northeast PHP 2015Architecting with Queues - Northeast PHP 2015
Architecting with Queues - Northeast PHP 2015
 
How to report a bug
How to report a bugHow to report a bug
How to report a bug
 
Working with Databases and MySQL
Working with Databases and MySQLWorking with Databases and MySQL
Working with Databases and MySQL
 
Don't Fear the Regex - Northeast PHP 2015
Don't Fear the Regex - Northeast PHP 2015Don't Fear the Regex - Northeast PHP 2015
Don't Fear the Regex - Northeast PHP 2015
 
Learning Regular Expressions for the Extraction of Product Attributes from E-...
Learning Regular Expressions for the Extraction of Product Attributes from E-...Learning Regular Expressions for the Extraction of Product Attributes from E-...
Learning Regular Expressions for the Extraction of Product Attributes from E-...
 

Ähnlich wie Regular expressions

Regular expressionfunction
Regular expressionfunctionRegular expressionfunction
Regular expressionfunctionADARSH BHATT
 
Manipulating strings
Manipulating stringsManipulating strings
Manipulating stringsNicole Ryan
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressionsKrishna Nanda
 
Regular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.netRegular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.netProgrammer Blog
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in PythonSujith Kumar
 
Chapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular ExpressionChapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular Expressionazzamhadeel89
 
A Brief Overview of (Static) Program Query Languages
A Brief Overview of (Static) Program Query LanguagesA Brief Overview of (Static) Program Query Languages
A Brief Overview of (Static) Program Query LanguagesKim Mens
 
Php intro by sami kz
Php intro by sami kzPhp intro by sami kz
Php intro by sami kzsami2244
 
P H P Part I, By Kian
P H P  Part  I,  By  KianP H P  Part  I,  By  Kian
P H P Part I, By Kianphelios
 
9780538745840 ppt ch03
9780538745840 ppt ch039780538745840 ppt ch03
9780538745840 ppt ch03Terry Yoast
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionskeeyre
 
Regular Expressions -- SAS and Perl
Regular Expressions -- SAS and PerlRegular Expressions -- SAS and Perl
Regular Expressions -- SAS and PerlMark Tabladillo
 
Unit 2 - Regular Expression.pptx
Unit 2 - Regular Expression.pptxUnit 2 - Regular Expression.pptx
Unit 2 - Regular Expression.pptxmythili213835
 

Ähnlich wie Regular expressions (20)

Regular expressionfunction
Regular expressionfunctionRegular expressionfunction
Regular expressionfunction
 
Manipulating strings
Manipulating stringsManipulating strings
Manipulating strings
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressions
 
Regular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.netRegular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.net
 
Perl_Part4
Perl_Part4Perl_Part4
Perl_Part4
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
Chapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular ExpressionChapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular Expression
 
Php Learning show
Php Learning showPhp Learning show
Php Learning show
 
Regex lecture
Regex lectureRegex lecture
Regex lecture
 
2013 - Andrei Zmievski: Clínica Regex
2013 - Andrei Zmievski: Clínica Regex2013 - Andrei Zmievski: Clínica Regex
2013 - Andrei Zmievski: Clínica Regex
 
A Brief Overview of (Static) Program Query Languages
A Brief Overview of (Static) Program Query LanguagesA Brief Overview of (Static) Program Query Languages
A Brief Overview of (Static) Program Query Languages
 
Php tutorial
Php tutorialPhp tutorial
Php tutorial
 
Php intro by sami kz
Php intro by sami kzPhp intro by sami kz
Php intro by sami kz
 
P H P Part I, By Kian
P H P  Part  I,  By  KianP H P  Part  I,  By  Kian
P H P Part I, By Kian
 
9780538745840 ppt ch03
9780538745840 ppt ch039780538745840 ppt ch03
9780538745840 ppt ch03
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular Expressions -- SAS and Perl
Regular Expressions -- SAS and PerlRegular Expressions -- SAS and Perl
Regular Expressions -- SAS and Perl
 
Unit 2 - Regular Expression.pptx
Unit 2 - Regular Expression.pptxUnit 2 - Regular Expression.pptx
Unit 2 - Regular Expression.pptx
 

Mehr von Nicole Ryan

Testing and Improving Performance
Testing and Improving PerformanceTesting and Improving Performance
Testing and Improving PerformanceNicole Ryan
 
Optimizing a website for search engines
Optimizing a website for search enginesOptimizing a website for search engines
Optimizing a website for search enginesNicole Ryan
 
Javascript programming using the document object model
Javascript programming using the document object modelJavascript programming using the document object model
Javascript programming using the document object modelNicole Ryan
 
Working with Video and Audio
Working with Video and AudioWorking with Video and Audio
Working with Video and AudioNicole Ryan
 
Working with Images
Working with ImagesWorking with Images
Working with ImagesNicole Ryan
 
Python Dictionaries and Sets
Python Dictionaries and SetsPython Dictionaries and Sets
Python Dictionaries and SetsNicole Ryan
 
Creating Visual Effects and Animation
Creating Visual Effects and AnimationCreating Visual Effects and Animation
Creating Visual Effects and AnimationNicole Ryan
 
Creating and Processing Web Forms
Creating and Processing Web FormsCreating and Processing Web Forms
Creating and Processing Web FormsNicole Ryan
 
Organizing Content with Lists and Tables
Organizing Content with Lists and TablesOrganizing Content with Lists and Tables
Organizing Content with Lists and TablesNicole Ryan
 
Social media and your website
Social media and your websiteSocial media and your website
Social media and your websiteNicole Ryan
 
Working with Links
Working with LinksWorking with Links
Working with LinksNicole Ryan
 
Formatting text with CSS
Formatting text with CSSFormatting text with CSS
Formatting text with CSSNicole Ryan
 
Laying Out Elements with CSS
Laying Out Elements with CSSLaying Out Elements with CSS
Laying Out Elements with CSSNicole Ryan
 
Getting Started with CSS
Getting Started with CSSGetting Started with CSS
Getting Started with CSSNicole Ryan
 
Structure Web Content
Structure Web ContentStructure Web Content
Structure Web ContentNicole Ryan
 
Getting Started with your Website
Getting Started with your WebsiteGetting Started with your Website
Getting Started with your WebsiteNicole Ryan
 
Chapter 12 Lecture: GUI Programming, Multithreading, and Animation
Chapter 12 Lecture: GUI Programming, Multithreading, and AnimationChapter 12 Lecture: GUI Programming, Multithreading, and Animation
Chapter 12 Lecture: GUI Programming, Multithreading, and AnimationNicole Ryan
 
Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2Nicole Ryan
 
Intro to Programming: Modularity
Intro to Programming: ModularityIntro to Programming: Modularity
Intro to Programming: ModularityNicole Ryan
 

Mehr von Nicole Ryan (20)

Testing and Improving Performance
Testing and Improving PerformanceTesting and Improving Performance
Testing and Improving Performance
 
Optimizing a website for search engines
Optimizing a website for search enginesOptimizing a website for search engines
Optimizing a website for search engines
 
Inheritance
InheritanceInheritance
Inheritance
 
Javascript programming using the document object model
Javascript programming using the document object modelJavascript programming using the document object model
Javascript programming using the document object model
 
Working with Video and Audio
Working with Video and AudioWorking with Video and Audio
Working with Video and Audio
 
Working with Images
Working with ImagesWorking with Images
Working with Images
 
Python Dictionaries and Sets
Python Dictionaries and SetsPython Dictionaries and Sets
Python Dictionaries and Sets
 
Creating Visual Effects and Animation
Creating Visual Effects and AnimationCreating Visual Effects and Animation
Creating Visual Effects and Animation
 
Creating and Processing Web Forms
Creating and Processing Web FormsCreating and Processing Web Forms
Creating and Processing Web Forms
 
Organizing Content with Lists and Tables
Organizing Content with Lists and TablesOrganizing Content with Lists and Tables
Organizing Content with Lists and Tables
 
Social media and your website
Social media and your websiteSocial media and your website
Social media and your website
 
Working with Links
Working with LinksWorking with Links
Working with Links
 
Formatting text with CSS
Formatting text with CSSFormatting text with CSS
Formatting text with CSS
 
Laying Out Elements with CSS
Laying Out Elements with CSSLaying Out Elements with CSS
Laying Out Elements with CSS
 
Getting Started with CSS
Getting Started with CSSGetting Started with CSS
Getting Started with CSS
 
Structure Web Content
Structure Web ContentStructure Web Content
Structure Web Content
 
Getting Started with your Website
Getting Started with your WebsiteGetting Started with your Website
Getting Started with your Website
 
Chapter 12 Lecture: GUI Programming, Multithreading, and Animation
Chapter 12 Lecture: GUI Programming, Multithreading, and AnimationChapter 12 Lecture: GUI Programming, Multithreading, and Animation
Chapter 12 Lecture: GUI Programming, Multithreading, and Animation
 
Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2
 
Intro to Programming: Modularity
Intro to Programming: ModularityIntro to Programming: Modularity
Intro to Programming: Modularity
 

Kürzlich hochgeladen

Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 

Kürzlich hochgeladen (20)

Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 

Regular expressions

  • 2. 2 PHP Programming with MySQL, 2nd Edition Regular Expressions Regular Expressions are patterns that are used for matching and manipulating strings according to specified rules PHP supports two types of regular expressions: POSIX Extended Perl Compatible Regular Expressions
  • 3. 3 PHP Programming with MySQL, 2nd Edition Regular Expressions
  • 4. 4 PHP Programming with MySQL, 2nd Edition Regular Expressions Pass to the preg_match() the regular expression pattern as the first argument and a string containing the text you want to search as the second argument preg_match(pattern, string);
  • 5. 5 PHP Programming with MySQL, 2nd Edition Writing Regular Expression Patterns A regular expression pattern is a special text string that describes a search pattern Regular expression patterns consist of literal characters and metacharacters, which are special characters that define the pattern-matching rules Regular expression patterns are enclosed in delimiters The most common delimiter is the forward slash (/)
  • 6. 6 PHP Programming with MySQL, 2nd Edition Regular Expression Patterns
  • 7. 7 PHP Programming with MySQL, 2nd Edition Matching Any Character A period (.) in a regular expression pattern specifies that the pattern must contain a value at the location of the period A return value of 0 indicates that the string does not match the pattern and 1 if it does $ZIP = "015"; preg_match("/...../", $ZIP); // returns 0 $ZIP = "01562"; preg_match("/...../", $ZIP); // returns 1
  • 8. 8 PHP Programming with MySQL, 2nd Edition Matching Characters An anchor specifies that the pattern must appear at a particular position in a string The ^ metacharacter anchors characters to the beginning of a string The $ metacharacter anchors characters to the end of a string $URL = "http://www.dongosselin.com"; preg_match("/^http/", $URL); // returns 1
  • 9. 9 PHP Programming with MySQL, 2nd Edition To specify an anchor at the beginning of a string, the pattern must begin with a ^ metcharacter $URL = "http://www.dongosselin.com"; eregi("^http", $URL); // returns 1; To specify an anchor at the end of a line, the pattern must end with the $ metacharacter $Identifier = "http://www.dongosselin.com"; eregi("com$", $Identifier); // returns 1 Matching Characters
  • 10. 10 PHP Programming with MySQL, 2nd Edition Matching Special Characters To match any metacharacters as literal values in a regular expression, escape the character with a backslash (in the following example, the last four characters in the string must be ‘.com’) $Identifier = http://www.dongosselin.com"; preg_match("/gov$/", $Identifier);//returns 0
  • 11. 11 PHP Programming with MySQL, 2nd Edition Specifying Quantity Meta characters that specify the quantity of a match are called quantifiers
  • 12. 12 PHP Programming with MySQL, 2nd Edition Specifying Quantity A question mark (?) quantifier specifies that the preceding character in the pattern is optional (in the following example, the string must begin with ‘http’ or ‘https’) $URL = "http://www.dongosselin.com"; preg_match("/^https?/", $URL); // returns 1
  • 13. 13 PHP Programming with MySQL, 2nd Edition Specifying Quantity • The addition(+) quantifier specifies that one or more sequential occurrences of the preceding characters match (in the following example, the string must have at least one character) $Name = "Don"; preg_match("/.+/", $Name); // returns 1
  • 14. 14 PHP Programming with MySQL, 2nd Edition Specifying Quantity • A asterisk (*) quantifier specifies that zero or more sequential occurrences of the preceding characters match (in the following example, the string must begin with one or more leading zeros) NumberString = "00125"; preg_match("/^0*/", $NumberString);//returns 1
  • 15. 15 PHP Programming with MySQL, 2nd Edition Specifying Quantity The { } quantifiers specify the number of times that a character must repeat sequentially (in the following example, the string must contain at least five characters) preg_match("/ZIP: .{5}$/", " ZIP: 01562"); // returns 1 The { } quantifiers can also specify the quantity as a range (in the following example, the string must contain between five and ten characters) preg_match("/(ZIP: .{5,10})$/", "ZIP: 01562-2607");// returns 1
  • 16. 16 PHP Programming with MySQL, 2nd Edition Specifying Subexpressions A subexpression or subpattern are a set of characters enclosed in parentheses are treated as a group In the example below, the 1 and the area code are optional, but if included must be in the following format: 1 (707) 555-1234 preg_match("/^(1 )?((.{3}) )?(.{3})(.{4})$/
  • 17. 17 PHP Programming with MySQL, 2nd Edition Defining Character Classes Character classes in regular expressions treat multiple characters as a single item. Characters enclosed with the ([]) metacharacters represent alternate characters that are allowed in a pattern match preg_match("/analy[sz]e/", "analyse");//returns 1 preg_match("/analy[sz]e/", "analyze");//returns 1 preg_match("/analy[sz]e/", "analyce");//returns 0
  • 18. 18 PHP Programming with MySQL, 2nd Edition Defining Character Classes The hyphen metacharacter (-) specifies a range of values in a character class. The following example ensures that A, B, C, D, or F are the only values assigned to the $LetterGrade variable: $LetterGrade = "B"; echo ereg("[A-DF]", $LetterGrade); // returns true
  • 19. 19 PHP Programming with MySQL, 2nd Edition Defining Character Classes (continued) The ^ metacharacter (placed immediately after the opening bracket of a character class) specifies optional characters to exclude in a pattern match. The following example excludes the letter E and G-Z from an acceptable pattern match in the $LetterGrade variable: $LetterGrade = "A"; echo ereg("[^EG-Z]", $LetterGrade); // returns true
  • 20. 20 PHP Programming with MySQL, 2nd Edition Defining Character Classes
  • 21. 21 PHP Programming with MySQL, 2nd Edition Matching Multiple Pattern Choices The | metacharacter is used to specify an alternate set of patterns The | metacharacter is essentially the same as using the OR operator to perform multiple evaluations in a conditional expression
  • 22. 22 PHP Programming with MySQL, 2nd Edition Pattern Modifiers Pattern modifiers are letters placed after the closing delimiter that change the default rules for interpreting matches – The pattern modifier, i, indicates that the case of the letter does not matter when searching – The pattern modifier, m, allows searches across newline characters – The pattern modifier, s, changes how the . (period) metacharacter works