SlideShare a Scribd company logo
Programmer Blog http://programmerblog.net
Regular Expressions in PHP
Programmer Blog http://programmerblog.net
Regular Expressions in PHP
 Regular Expressions
 Regular expressions provide the foundation for describing or matching data according to
defined syntax rules. A regular expression is nothing more than a pattern of characters itself,
matched against a certain parcel of text.
 PHP offers functions specific to two sets of regular expression functions, each
corresponding
to a certain type of regular expression: POSIX and Perl-style.
 Regular Expression Syntax (POSIX)
Programmer Blog http://programmerblog.net
Regular Expressions in PHP
 Quantifiers
 The frequency or position of bracketed character sequences and single characters can be denoted by a
special character, with each special character having a specific connotation. The +, *, ?, {occurrence_range}
 The frequency or position of bracketed character sequences and single characters can be denoted by a
special character, with each special character having a specific connotation. The +, *, ?,
{occurrence_range} , and $
 Brackets
 Brackets ([]) have a special meaning when used in the context of regular expressions, which are used to
find a range of characters.
 The regular expression [php] will find any string containing the character p or h
 [0-9] matches any decimal digit from 0 through 9.
 [a-z] matches any character from lowercase a through lowercase z.
 [A-Z] matches any character from uppercase A through uppercase Z.
 [A-Za-z] matches any character from uppercase A through lowercase z.
 p+ matches any string containing at least one p.
 p* matches any string containing zero or more p’s.
 p? matches any string containing zero or one p.
 p{2} matches any string containing a sequence of two p’s.
 p{2,3} matches any string containing a sequence of two or three p’s.
Programmer Blog http://programmerblog.net
Regular Expressions
 p{2,} matches any string containing a sequence of at least two p’s.
 p$ matches any string with p at the end of it.
 Still other flags can precede and be inserted before and within a character sequence:
 ^p matches any string with p at the beginning of it.
 [^a-zA-Z] matches any string not containing any of the characters ranging from a
 through z and A through Z.
 p.p matches any string containing p, followed by any character, in turn followed by
 another p.
 ^.{2}$ matches any string containing exactly two characters.
 <b>(.*)</b> matches any string enclosed within <b> and </b> (presumably HTML bold tags).
 p(hp)* matches any string containing a p followed by zero or more instances of the sequence hp.
 Search for these special characters in strings
 The characters must be escaped with a backslash (). For example, if you wanted to search for a dollar
amount, a plausible regular expression would be as follows: ([$])([0-9]+); that is, a dollar sign followed by
one or more integers.
 $42, $560, and $3.
 Predefined Character Ranges (Character Classes)
 For your programming convenience, several predefined character ranges, also known as character
 classes, are available.
 [:alpha:]: Lowercase and uppercase alphabetical characters. This can also be specified as [A-Za-z].
Programmer Blog http://programmerblog.net
Regular Expressions
 Regular Expression Will match...
 foo The string "foo"
 ^foo "foo" at the start of a string
 foo$ "foo" at the end of a string
 ^foo$ "foo" when it is alone on a string
 [abc] a, b, or c
 [a-z] Any lowercase letter
 [^A-Z] Any character that is not a uppercase letter
 (gif|jpg) Matches either "gif" or "jpeg"
 [a-z]+ One or more lowercase letters
 [0-9.-] Аny number, dot, or minus sign
 ^[a-zA-Z0-9_]{1,}$ Any word of at least one letter, number or _
 ([wx])([yz]) wy, wz, xy, or xz
 [^A-Za-z0-9] Any symbol (not a number or a letter)
 ([A-Z]{3}|[0-9]{4}) Matches three letters or four numbers
Programmer Blog http://programmerblog.net
Regular Expressions
 Regular Expression Syntax (Perl Style)
 The developers of PHP felt that instead of reinventing the regular expression wheel, so to speak, they
should make the famed Perl regular expression syntax available to PHP users, thus the Perl-style functions.
 . Match any character
 ˆ Match the start of the string
 $ Match the end of the string
 s Match any whitespace character
 d Match any digit
 w Match any “word” character
 A: Matches only at the beginning of the string.
 b: Matches a word boundary.
 B: Matches anything but a word boundary.
 A caret (^) character at the beginning of a regular expression indicates that it must match the beginning of
the string.
 The dot (.) metacharacter matches any single character except newline (). So, the pattern h.t matches hat,
hothit, hut, h7t, etc.
 The vertical pipe (|) metacharacter is used for alternatives in a regular expression. It behaves much like a
logical OR operator
Programmer Blog http://programmerblog.net
Regular Expressions
 The metacharacters +, *, ?, and {} affect the number of times a pattern should be matched.
 + means "Match one or more of the preceding expression", * means "Match zero or more of the preceding
expression",
 ? means "Match zero or one of the preceding expression".
 Curly braces {} can be used differently. With a single integer,
 {n} means "match exactly n occurrences of the preceding expression", with one integer and a comma, {n,}
means "match n or more occurrences of the preceding expression", and with two comma-separated integers
{n,m} means "match the previous character if it occurs at least n times, but no more than m times".
 /food/
 Notice that the string food is enclosed between two forward slashes. Just like with POSIX regular
expressions, you can build a more complex string through the use of quantifiers:
 /fo+/
 This will match fo followed by one or more characters. Some potential matches include
 food, fool, and fo4
 PHP’s Regular Expression Functions (Perl Compatible)
 preg_grep()
 array preg_grep (string pattern, array input [, flags])
 The preg_grep() function searches all elements of the array input, returning an array consisting of
 all elements matching pattern.
Programmer Blog http://programmerblog.net
Regular Expressions
 $foods = array("pasta", "steak", "fish", "potatoes");
 $food = preg_grep("/^p/", $foods);
 print_r($food);
 preg_match()
 int preg_match (string pattern, string string [, array matches]
 [, int flags [, int offset]]])
 The preg_match() function searches string for pattern, returning TRUE if it exists and FALSE
 otherwise. The optional input parameter pattern_array can contain various sections of the
 subpatterns contained in the search pattern
 <?php
 $line = "Vim is the greatest word processor ever created!";
 if (preg_match("/bVimb/i", $line, $match)) print "Match found!";
 ?>
 preg_match_all()
 int preg_match_all (string pattern, string string, array pattern_array
 [, int order])
 The preg_match_all() function matches all occurrences of pattern in string, assigning each occurrence to
array pattern_array in the order you specify via the optional input parameter order.
Programmer Blog http://programmerblog.net
Regular Expressions
 preg_replace_callback()
 mixed preg_replace_callback(mixed pattern, callback callback, mixed str
 [, int limit])
 Rather than handling the replacement procedure itself, the preg_replace_callback() function delegates the
string-replacement procedure to some other user-defined function.
 preg_split()
 array preg_split (string pattern, string string [, int limit [, int flags]])
 The preg_split() function operates like pattern can also be defined in terms of a regular expression.
 <?php
 $delimitedText = "+Jason+++Gilmore+++++++++++Columbus+++OH";
 $fields = preg_split("/+{1,}/", $delimitedText);
 foreach($fields as $field)
 echo $field."<br />";
 ?>
 OUTPUT:
 Jason
 Gilmore
 Columbus
 OH
Programmer Blog http://programmerblog.net
Regular Expressions
 ereg($p,$str)
 ergi($p,$str)
 $ereg_replace($p,$r, $str)
 sql_regcase($p)
 split($p,$str)
 preg_match();
 pre_match_all()
 preg_replace()
 preg_split()

More Related Content

What's hot

Regular Expression
Regular ExpressionRegular Expression
Regular Expression
Lambert Lum
 
Andrei's Regex Clinic
Andrei's Regex ClinicAndrei's Regex Clinic
Andrei's Regex Clinic
Andrei Zmievski
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaghu nath
 
Grep
GrepGrep
Regular expressions and php
Regular expressions and phpRegular expressions and php
Regular expressions and php
David Stockton
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
Shree M.L.Kakadiya MCA mahila college, Amreli
 
2013 - Andrei Zmievski: Clínica Regex
2013 - Andrei Zmievski: Clínica Regex2013 - Andrei Zmievski: Clínica Regex
2013 - Andrei Zmievski: Clínica Regex
PHP Conference Argentina
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in pythonPython advanced 2. regular expression in python
Python advanced 2. regular expression in pythonJohn(Qiang) Zhang
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20
Max Kleiner
 
Introduction to Regular Expressions
Introduction to Regular ExpressionsIntroduction to Regular Expressions
Introduction to Regular Expressions
Jesse Anderson
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
Prof. Wim Van Criekinge
 
Grep - A powerful search utility
Grep - A powerful search utilityGrep - A powerful search utility
Grep - A powerful search utility
Nirajan Pant
 
15 practical grep command examples in linux
15 practical grep command examples in linux15 practical grep command examples in linux
15 practical grep command examples in linuxTeja Bheemanapally
 
Java: Regular Expression
Java: Regular ExpressionJava: Regular Expression
Java: Regular ExpressionMasudul Haque
 
Looking for Patterns
Looking for PatternsLooking for Patterns
Looking for Patterns
Keith Wright
 
Introduction to regular expressions
Introduction to regular expressionsIntroduction to regular expressions
Introduction to regular expressions
Ben Brumfield
 
Python (regular expression)
Python (regular expression)Python (regular expression)
Python (regular expression)
Chirag Shetty
 
Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014
Prof. Wim Van Criekinge
 

What's hot (20)

Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
Andrei's Regex Clinic
Andrei's Regex ClinicAndrei's Regex Clinic
Andrei's Regex Clinic
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Grep
GrepGrep
Grep
 
Regular expressions and php
Regular expressions and phpRegular expressions and php
Regular expressions and php
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
2013 - Andrei Zmievski: Clínica Regex
2013 - Andrei Zmievski: Clínica Regex2013 - Andrei Zmievski: Clínica Regex
2013 - Andrei Zmievski: Clínica Regex
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in pythonPython advanced 2. regular expression in python
Python advanced 2. regular expression in python
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20
 
Introduction to Regular Expressions
Introduction to Regular ExpressionsIntroduction to Regular Expressions
Introduction to Regular Expressions
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
 
Grep - A powerful search utility
Grep - A powerful search utilityGrep - A powerful search utility
Grep - A powerful search utility
 
15 practical grep command examples in linux
15 practical grep command examples in linux15 practical grep command examples in linux
15 practical grep command examples in linux
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Java: Regular Expression
Java: Regular ExpressionJava: Regular Expression
Java: Regular Expression
 
Looking for Patterns
Looking for PatternsLooking for Patterns
Looking for Patterns
 
Introduction to regular expressions
Introduction to regular expressionsIntroduction to regular expressions
Introduction to regular expressions
 
Bioinformatica p2-p3-introduction
Bioinformatica p2-p3-introductionBioinformatica p2-p3-introduction
Bioinformatica p2-p3-introduction
 
Python (regular expression)
Python (regular expression)Python (regular expression)
Python (regular expression)
 
Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014
 

Similar to Regular Expressions in PHP, MySQL by programmerblog.net

Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptx
DurgaNayak4
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regex
Jalpesh Vasa
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
Ahmed Swilam
 
Regex Basics
Regex BasicsRegex Basics
Regex Basics
Jeremy Coates
 
Regex posix
Regex posixRegex posix
Regex posix
sana mateen
 
Regular expressions in oracle
Regular expressions in oracleRegular expressions in oracle
Regular expressions in oracle
Logan Palanisamy
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaj Gupta
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressions
Krishna Nanda
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
Muthuselvam RS
 
Introduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_RIntroduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_RHellen Gakuruh
 
Unit 1-array,lists and hashes
Unit 1-array,lists and hashesUnit 1-array,lists and hashes
Unit 1-array,lists and hashes
sana mateen
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
sana mateen
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
sana mateen
 
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdfFUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
Bryan Alejos
 
Php Chapter 4 Training
Php Chapter 4 TrainingPhp Chapter 4 Training
Php Chapter 4 Training
Chris Chubb
 
Chapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular ExpressionChapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular Expression
azzamhadeel89
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
Sujith Kumar
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Nicole Ryan
 
Regular expressionfunction
Regular expressionfunctionRegular expressionfunction
Regular expressionfunctionADARSH BHATT
 
Regular Expressions in Stata
Regular Expressions in StataRegular Expressions in Stata
Regular Expressions in Stata
John Ong'ala Lunalo
 

Similar to Regular Expressions in PHP, MySQL by programmerblog.net (20)

Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptx
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regex
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
Regex Basics
Regex BasicsRegex Basics
Regex Basics
 
Regex posix
Regex posixRegex posix
Regex posix
 
Regular expressions in oracle
Regular expressions in oracleRegular expressions in oracle
Regular expressions in oracle
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressions
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
Introduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_RIntroduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_R
 
Unit 1-array,lists and hashes
Unit 1-array,lists and hashesUnit 1-array,lists and hashes
Unit 1-array,lists and hashes
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
 
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdfFUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
 
Php Chapter 4 Training
Php Chapter 4 TrainingPhp Chapter 4 Training
Php Chapter 4 Training
 
Chapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular ExpressionChapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular Expression
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular expressionfunction
Regular expressionfunctionRegular expressionfunction
Regular expressionfunction
 
Regular Expressions in Stata
Regular Expressions in StataRegular Expressions in Stata
Regular Expressions in Stata
 

Recently uploaded

Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 

Recently uploaded (20)

Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 

Regular Expressions in PHP, MySQL by programmerblog.net

  • 2. Programmer Blog http://programmerblog.net Regular Expressions in PHP  Regular Expressions  Regular expressions provide the foundation for describing or matching data according to defined syntax rules. A regular expression is nothing more than a pattern of characters itself, matched against a certain parcel of text.  PHP offers functions specific to two sets of regular expression functions, each corresponding to a certain type of regular expression: POSIX and Perl-style.  Regular Expression Syntax (POSIX)
  • 3. Programmer Blog http://programmerblog.net Regular Expressions in PHP  Quantifiers  The frequency or position of bracketed character sequences and single characters can be denoted by a special character, with each special character having a specific connotation. The +, *, ?, {occurrence_range}  The frequency or position of bracketed character sequences and single characters can be denoted by a special character, with each special character having a specific connotation. The +, *, ?, {occurrence_range} , and $  Brackets  Brackets ([]) have a special meaning when used in the context of regular expressions, which are used to find a range of characters.  The regular expression [php] will find any string containing the character p or h  [0-9] matches any decimal digit from 0 through 9.  [a-z] matches any character from lowercase a through lowercase z.  [A-Z] matches any character from uppercase A through uppercase Z.  [A-Za-z] matches any character from uppercase A through lowercase z.  p+ matches any string containing at least one p.  p* matches any string containing zero or more p’s.  p? matches any string containing zero or one p.  p{2} matches any string containing a sequence of two p’s.  p{2,3} matches any string containing a sequence of two or three p’s.
  • 4. Programmer Blog http://programmerblog.net Regular Expressions  p{2,} matches any string containing a sequence of at least two p’s.  p$ matches any string with p at the end of it.  Still other flags can precede and be inserted before and within a character sequence:  ^p matches any string with p at the beginning of it.  [^a-zA-Z] matches any string not containing any of the characters ranging from a  through z and A through Z.  p.p matches any string containing p, followed by any character, in turn followed by  another p.  ^.{2}$ matches any string containing exactly two characters.  <b>(.*)</b> matches any string enclosed within <b> and </b> (presumably HTML bold tags).  p(hp)* matches any string containing a p followed by zero or more instances of the sequence hp.  Search for these special characters in strings  The characters must be escaped with a backslash (). For example, if you wanted to search for a dollar amount, a plausible regular expression would be as follows: ([$])([0-9]+); that is, a dollar sign followed by one or more integers.  $42, $560, and $3.  Predefined Character Ranges (Character Classes)  For your programming convenience, several predefined character ranges, also known as character  classes, are available.  [:alpha:]: Lowercase and uppercase alphabetical characters. This can also be specified as [A-Za-z].
  • 5. Programmer Blog http://programmerblog.net Regular Expressions  Regular Expression Will match...  foo The string "foo"  ^foo "foo" at the start of a string  foo$ "foo" at the end of a string  ^foo$ "foo" when it is alone on a string  [abc] a, b, or c  [a-z] Any lowercase letter  [^A-Z] Any character that is not a uppercase letter  (gif|jpg) Matches either "gif" or "jpeg"  [a-z]+ One or more lowercase letters  [0-9.-] Аny number, dot, or minus sign  ^[a-zA-Z0-9_]{1,}$ Any word of at least one letter, number or _  ([wx])([yz]) wy, wz, xy, or xz  [^A-Za-z0-9] Any symbol (not a number or a letter)  ([A-Z]{3}|[0-9]{4}) Matches three letters or four numbers
  • 6. Programmer Blog http://programmerblog.net Regular Expressions  Regular Expression Syntax (Perl Style)  The developers of PHP felt that instead of reinventing the regular expression wheel, so to speak, they should make the famed Perl regular expression syntax available to PHP users, thus the Perl-style functions.  . Match any character  ˆ Match the start of the string  $ Match the end of the string  s Match any whitespace character  d Match any digit  w Match any “word” character  A: Matches only at the beginning of the string.  b: Matches a word boundary.  B: Matches anything but a word boundary.  A caret (^) character at the beginning of a regular expression indicates that it must match the beginning of the string.  The dot (.) metacharacter matches any single character except newline (). So, the pattern h.t matches hat, hothit, hut, h7t, etc.  The vertical pipe (|) metacharacter is used for alternatives in a regular expression. It behaves much like a logical OR operator
  • 7. Programmer Blog http://programmerblog.net Regular Expressions  The metacharacters +, *, ?, and {} affect the number of times a pattern should be matched.  + means "Match one or more of the preceding expression", * means "Match zero or more of the preceding expression",  ? means "Match zero or one of the preceding expression".  Curly braces {} can be used differently. With a single integer,  {n} means "match exactly n occurrences of the preceding expression", with one integer and a comma, {n,} means "match n or more occurrences of the preceding expression", and with two comma-separated integers {n,m} means "match the previous character if it occurs at least n times, but no more than m times".  /food/  Notice that the string food is enclosed between two forward slashes. Just like with POSIX regular expressions, you can build a more complex string through the use of quantifiers:  /fo+/  This will match fo followed by one or more characters. Some potential matches include  food, fool, and fo4  PHP’s Regular Expression Functions (Perl Compatible)  preg_grep()  array preg_grep (string pattern, array input [, flags])  The preg_grep() function searches all elements of the array input, returning an array consisting of  all elements matching pattern.
  • 8. Programmer Blog http://programmerblog.net Regular Expressions  $foods = array("pasta", "steak", "fish", "potatoes");  $food = preg_grep("/^p/", $foods);  print_r($food);  preg_match()  int preg_match (string pattern, string string [, array matches]  [, int flags [, int offset]]])  The preg_match() function searches string for pattern, returning TRUE if it exists and FALSE  otherwise. The optional input parameter pattern_array can contain various sections of the  subpatterns contained in the search pattern  <?php  $line = "Vim is the greatest word processor ever created!";  if (preg_match("/bVimb/i", $line, $match)) print "Match found!";  ?>  preg_match_all()  int preg_match_all (string pattern, string string, array pattern_array  [, int order])  The preg_match_all() function matches all occurrences of pattern in string, assigning each occurrence to array pattern_array in the order you specify via the optional input parameter order.
  • 9. Programmer Blog http://programmerblog.net Regular Expressions  preg_replace_callback()  mixed preg_replace_callback(mixed pattern, callback callback, mixed str  [, int limit])  Rather than handling the replacement procedure itself, the preg_replace_callback() function delegates the string-replacement procedure to some other user-defined function.  preg_split()  array preg_split (string pattern, string string [, int limit [, int flags]])  The preg_split() function operates like pattern can also be defined in terms of a regular expression.  <?php  $delimitedText = "+Jason+++Gilmore+++++++++++Columbus+++OH";  $fields = preg_split("/+{1,}/", $delimitedText);  foreach($fields as $field)  echo $field."<br />";  ?>  OUTPUT:  Jason  Gilmore  Columbus  OH
  • 10. Programmer Blog http://programmerblog.net Regular Expressions  ereg($p,$str)  ergi($p,$str)  $ereg_replace($p,$r, $str)  sql_regcase($p)  split($p,$str)  preg_match();  pre_match_all()  preg_replace()  preg_split()