SlideShare a Scribd company logo
5
Most read
8
Most read
10
Most read
REGULAR
EXPRESSION
What is Regular Expression
• Regular Expressions, commonly known as "regex" or
"RegExp", are a specially formatted text strings used to
find patterns in text.
• Regular expressions are one of the most powerful tools
available today for effective and efficient text processing
and manipulations.
• For example, it can be used to verify whether the format
of data i.e. name, email, phone number, etc. entered by
the user was correct or not, find or replace matching
string within text content, and so on.
PHP's built-in pattern-matching functions
Function What it Does
preg_match() Perform a regular expression
match.
preg_match_all() Perform a global regular
expression match.
preg_replace() Perform a regular expression
search and replace.
preg_grep() Returns the elements of the input
array that matched the pattern.
preg_split() Splits up a string into substrings
using a regular expression.
Character Classes
• Square brackets surrounding a pattern of characters are
called a character class e.g. [abc].
RegExp What it Does
[abc] Matches any one of the characters a, b,
or c.
[^abc] Matches any one character other than a,
b, or c.
[a-z] Matches any one character from
lowercase a to lowercase z.
[A-Z] Matches any one character from
uppercase a to uppercase z.
[a-Z] Matches any one character from
lowercase a to uppercase Z.
[0-9] Matches a single digit between 0 and 9.
[a-z0-9] Matches a single character between a
and z or between 0 and 9.
Predefined Character Classes
• Some character classes such as digits, letters, and
whitespaces are used so frequently that there are shortcut
names for them.
Shortcut What it Does
. Matches any single character except newline n.
d matches any digit character. Same as [0-9]
D Matches any non-digit character. Same as [^0-9]
s Matches any whitespace character (space, tab, newline. Same as [ tn]
S Matches any non-whitespace character. Same as [^ tnr]
w Matches any word character (definned as a to z, A to Z,0 to 9, and the
underscore). Same as [a-zA-Z_0-9]
W Matches any non-word character. Same as [^a-zA-Z_0-9]
Position Anchors
• There are certain situations where you want to match at
the beginning or end of a line, word, or string. To do this
you can use anchors.
• Two common anchors are caret (^) which represent the
start of the string, and the dollar ($) sign which represent
the end of the string.
RegExp What it Does
^p Matches the letter p at the
beginning of a line.
p$ Matches the letter p at the end of
a line.
Pattern Modifiers
• A pattern modifier allows you to control the way a pattern
match is handled.
• Pattern modifiers are placed directly after the regular
expression, for example, if you want to search for a
pattern in a case-insensitive manner, you can use the i
modifier, like this: /pattern/i. The following table lists some
of the most commonly used pattern modifiers.
Pattern Modifiers(contd.)
Modifier What it Does
i Makes the match case-insensitive manner.
m Changes the behavior of ^ and $ to match
against a newline boundary (i.e. start or end
of each line within a multiline string),
instead of a string boundary.
g Perform a global match i.e. finds all
occurrences.
o Evaluates the expression only once.
s Changes the behavior of . (dot) to match all
characters, including newlines.
x Allows you to use whitespace and
comments within a regular expression for
clarity.
Whatispreg_matchinPHP?
• preg_match in PHP function is used to search the string for
a pattern and return a Boolean value.
• The search generally starts from the initial character of the
string. An optional parameter 'offset' is used to specify the
position from where the search begins, that is, an alternate
position from which the search can start. Thus, preg_match
in the PHP function looks for a match in the string.
• Syntax of preg_match in PHP
The syntax of the function is:
preg_match(pattern, input, matches, flags, offset)
ParameterValuesandUseCase
• preg_match in PHP has five parameters. All of them are defined below:
• pattern- This parameter has the pattern that needs to be searched in the
string.
• input- This parameter has the string that needs to be searched.
• matches- If there is a match, this parameter contains the search result.
• $matches[0] contains the text matching full pattern;
• $matches[1] contains text that matches with the first recognized
parenthesized subpattern which goes on.
• flag- The flag parameter can contain either flag:
• PREG_UNMATCHED_AS_NULL: When this flag is passed, subpatterns
that are not matched are reported as NULL, whereas others are reported
empty.
• PREG_OFFSET_CAPTURE: When this flag is passed, the append string
offset will be returned for every match.
• offset- As mentioned earlier, this parameter specifies the position (in bytes)
from where the search needs to start. If not specified, search by default
starts from the beginning.
Task
• Using a regular expression for doing a case-insensitive
search in the given string.
• Using PREG_OFFSET_CAPTURE to find the position of
the string where the match was found .
• To validate whether the supplied value is an integer or not,
as well as whether it lies within the range of 0 to 100 or
not.

More Related Content

PPT
Regular Expressions in PHP, MySQL by programmerblog.net
PDF
Don't Fear the Regex LSP15
PPTX
Php pattern matching
PDF
Don't Fear the Regex - CapitalCamp/GovDays 2014
PDF
Don't Fear the Regex - Northeast PHP 2015
PDF
Don't Fear the Regex WordCamp DC 2017
PDF
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
PPT
Php String And Regular Expressions
Regular Expressions in PHP, MySQL by programmerblog.net
Don't Fear the Regex LSP15
Php pattern matching
Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - Northeast PHP 2015
Don't Fear the Regex WordCamp DC 2017
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
Php String And Regular Expressions

Similar to Regular expressions in php programming language.pptx (20)

PPT
Class 5 - PHP Strings
ODP
Regex Presentation
ODP
Regex Presentation
PPT
regex.ppt
PPT
Regular Expressions 2007
PPTX
Regular Expressions in PHP
PPT
Regex Basics
PDF
Basta mastering regex power
PPT
Bioinformatica 06-10-2011-p2 introduction
ODP
Introduction To Regex in Lasso 8.5
PDF
2013 - Andrei Zmievski: Clínica Regex
PPT
Php Chapter 4 Training
PDF
/Regex makes me want to (weep_give up_(╯°□°)╯︵ ┻━┻)/i (for 2024 CascadiaPHP)
PPTX
Unit 1-strings,patterns and regular expressions
PPTX
Strings,patterns and regular expressions in perl
PPTX
Regular expressions
PPT
Regular Expressions grep and egrep
DOCX
Regular expressionfunction
Class 5 - PHP Strings
Regex Presentation
Regex Presentation
regex.ppt
Regular Expressions 2007
Regular Expressions in PHP
Regex Basics
Basta mastering regex power
Bioinformatica 06-10-2011-p2 introduction
Introduction To Regex in Lasso 8.5
2013 - Andrei Zmievski: Clínica Regex
Php Chapter 4 Training
/Regex makes me want to (weep_give up_(╯°□°)╯︵ ┻━┻)/i (for 2024 CascadiaPHP)
Unit 1-strings,patterns and regular expressions
Strings,patterns and regular expressions in perl
Regular expressions
Regular Expressions grep and egrep
Regular expressionfunction
Ad

Recently uploaded (20)

PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
DOCX
573137875-Attendance-Management-System-original
PPT
Mechanical Engineering MATERIALS Selection
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
web development for engineering and engineering
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Construction Project Organization Group 2.pptx
PPTX
Welding lecture in detail for understanding
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
UNIT 4 Total Quality Management .pptx
CH1 Production IntroductoryConcepts.pptx
573137875-Attendance-Management-System-original
Mechanical Engineering MATERIALS Selection
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
CYBER-CRIMES AND SECURITY A guide to understanding
web development for engineering and engineering
bas. eng. economics group 4 presentation 1.pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Model Code of Practice - Construction Work - 21102022 .pdf
Foundation to blockchain - A guide to Blockchain Tech
Construction Project Organization Group 2.pptx
Welding lecture in detail for understanding
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
R24 SURVEYING LAB MANUAL for civil enggi
Embodied AI: Ushering in the Next Era of Intelligent Systems
Ad

Regular expressions in php programming language.pptx

  • 2. What is Regular Expression • Regular Expressions, commonly known as "regex" or "RegExp", are a specially formatted text strings used to find patterns in text. • Regular expressions are one of the most powerful tools available today for effective and efficient text processing and manipulations. • For example, it can be used to verify whether the format of data i.e. name, email, phone number, etc. entered by the user was correct or not, find or replace matching string within text content, and so on.
  • 3. PHP's built-in pattern-matching functions Function What it Does preg_match() Perform a regular expression match. preg_match_all() Perform a global regular expression match. preg_replace() Perform a regular expression search and replace. preg_grep() Returns the elements of the input array that matched the pattern. preg_split() Splits up a string into substrings using a regular expression.
  • 4. Character Classes • Square brackets surrounding a pattern of characters are called a character class e.g. [abc]. RegExp What it Does [abc] Matches any one of the characters a, b, or c. [^abc] Matches any one character other than a, b, or c. [a-z] Matches any one character from lowercase a to lowercase z. [A-Z] Matches any one character from uppercase a to uppercase z. [a-Z] Matches any one character from lowercase a to uppercase Z. [0-9] Matches a single digit between 0 and 9. [a-z0-9] Matches a single character between a and z or between 0 and 9.
  • 5. Predefined Character Classes • Some character classes such as digits, letters, and whitespaces are used so frequently that there are shortcut names for them. Shortcut What it Does . Matches any single character except newline n. d matches any digit character. Same as [0-9] D Matches any non-digit character. Same as [^0-9] s Matches any whitespace character (space, tab, newline. Same as [ tn] S Matches any non-whitespace character. Same as [^ tnr] w Matches any word character (definned as a to z, A to Z,0 to 9, and the underscore). Same as [a-zA-Z_0-9] W Matches any non-word character. Same as [^a-zA-Z_0-9]
  • 6. Position Anchors • There are certain situations where you want to match at the beginning or end of a line, word, or string. To do this you can use anchors. • Two common anchors are caret (^) which represent the start of the string, and the dollar ($) sign which represent the end of the string. RegExp What it Does ^p Matches the letter p at the beginning of a line. p$ Matches the letter p at the end of a line.
  • 7. Pattern Modifiers • A pattern modifier allows you to control the way a pattern match is handled. • Pattern modifiers are placed directly after the regular expression, for example, if you want to search for a pattern in a case-insensitive manner, you can use the i modifier, like this: /pattern/i. The following table lists some of the most commonly used pattern modifiers.
  • 8. Pattern Modifiers(contd.) Modifier What it Does i Makes the match case-insensitive manner. m Changes the behavior of ^ and $ to match against a newline boundary (i.e. start or end of each line within a multiline string), instead of a string boundary. g Perform a global match i.e. finds all occurrences. o Evaluates the expression only once. s Changes the behavior of . (dot) to match all characters, including newlines. x Allows you to use whitespace and comments within a regular expression for clarity.
  • 9. Whatispreg_matchinPHP? • preg_match in PHP function is used to search the string for a pattern and return a Boolean value. • The search generally starts from the initial character of the string. An optional parameter 'offset' is used to specify the position from where the search begins, that is, an alternate position from which the search can start. Thus, preg_match in the PHP function looks for a match in the string. • Syntax of preg_match in PHP The syntax of the function is: preg_match(pattern, input, matches, flags, offset)
  • 10. ParameterValuesandUseCase • preg_match in PHP has five parameters. All of them are defined below: • pattern- This parameter has the pattern that needs to be searched in the string. • input- This parameter has the string that needs to be searched. • matches- If there is a match, this parameter contains the search result. • $matches[0] contains the text matching full pattern; • $matches[1] contains text that matches with the first recognized parenthesized subpattern which goes on. • flag- The flag parameter can contain either flag: • PREG_UNMATCHED_AS_NULL: When this flag is passed, subpatterns that are not matched are reported as NULL, whereas others are reported empty. • PREG_OFFSET_CAPTURE: When this flag is passed, the append string offset will be returned for every match. • offset- As mentioned earlier, this parameter specifies the position (in bytes) from where the search needs to start. If not specified, search by default starts from the beginning.
  • 11. Task • Using a regular expression for doing a case-insensitive search in the given string. • Using PREG_OFFSET_CAPTURE to find the position of the string where the match was found . • To validate whether the supplied value is an integer or not, as well as whether it lies within the range of 0 to 100 or not.