KEMBAR78
Regular expressions in php programming language.pptx
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.

Regular expressions in php programming language.pptx

  • 1.
  • 2.
    What is RegularExpression • 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-matchingfunctions 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 • Squarebrackets 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 • Thereare 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 • Apattern 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 Whatit 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 inPHP 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 inPHP 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 aregular 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.