Introduction PHP and REGEX Jussi Pohjolainen TAMK University of Applied Sciences
Regular Expressions <ul><li>Designed to help working with strings in UNIX environment </li></ul><ul><li>Easy way to find a...
Basic Syntax <ul><li>In Regex, one uses patterns </li></ul><ul><li>Pattern can hold </li></ul><ul><ul><li>Normal character...
ereg Functions in PHP <ul><li>ereg-functions uses POSIX regex: </li></ul><ul><ul><li>int ereg (string pattern, string subj...
Example <ul><li>// POSIX REGEX </li></ul><ul><li>$pattern = &quot;^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+.[a-zA-Z.]{2,5}$&quot;; </...
Example 2 <ul><li>// POSIX REGEX </li></ul><ul><li>$pattern = &quot;^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+.[a-zA-Z.]{2,5}$&quot;; ...
preg function set <ul><li>Perl compatible REGEX: </li></ul><ul><ul><li>int preg_match  ( string $pattern  , string $subjec...
Example <ul><li>// PERL REGEX </li></ul><ul><li>$pattern = &quot;/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+.[a-zA-Z.]{2,5}$/i&quot;; ...
Perl Regex Examples <ul><li>Basic usage: /pattern/modifiers </li></ul><ul><li>Examples:  </li></ul><ul><ul><li>/ha jo/ mat...
Some PERL regex rules REGEX Explanation Example . Any single character /a.c/ matches &quot;abc&quot; or &quot;afc&quot; [ ...
Examples <ul><li>/.ala/ </li></ul><ul><ul><li>ka vala mpi </li></ul></ul><ul><li>/. Älä/ </li></ul><ul><ul><li>Nyt se lopp...
Examples <ul><li>/^begin and end$/ </li></ul><ul><ul><li>begin and end </li></ul></ul><ul><li>/one|two|three/ </li></ul><u...
More Perl REGEX Rules REGEX Explanation w [a-zA-Z0-9_] W ^ [a-zA-Z0-9_] 
 New Line s Whitespace / Empty S Not whitespaces ...
Examples <ul><li>/^[+-]?d+.?d*$/ </li></ul><ul><li>/^d{1,2}.d{1,2}.?d{4}?$/ </li></ul><ul><li>/^(d{1,2}):(d{1,2}).(d{1,2})...
Upcoming SlideShare
Loading in...5
×

PHP Regular Expressions

4,049

Published on

Published in: Education, Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,049
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

PHP Regular Expressions

  1. 1. Introduction PHP and REGEX Jussi Pohjolainen TAMK University of Applied Sciences
  2. 2. Regular Expressions <ul><li>Designed to help working with strings in UNIX environment </li></ul><ul><li>Easy way to find a pattern in a string and/or replace it if you want </li></ul><ul><li>Very powerful tool </li></ul><ul><li>Two types: POSIX and Perl Compatible </li></ul>
  3. 3. Basic Syntax <ul><li>In Regex, one uses patterns </li></ul><ul><li>Pattern can hold </li></ul><ul><ul><li>Normal characters </li></ul></ul><ul><ul><li>Start and end indicators as ^ and $ </li></ul></ul><ul><ul><li>Count indicators like +, - , ? </li></ul></ul><ul><ul><li>Logical operators, like | </li></ul></ul><ul><ul><li>Grouping with {}, (), [] </li></ul></ul><ul><li>Example (Perl-compatible) </li></ul><ul><ul><li>/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+.[a-zA-Z.]{2,5}$/ </li></ul></ul>
  4. 4. ereg Functions in PHP <ul><li>ereg-functions uses POSIX regex: </li></ul><ul><ul><li>int ereg (string pattern, string subject [, array groups]) </li></ul></ul><ul><ul><li>int eregi (string pattern, string subject [, array groups]) </li></ul></ul><ul><ul><li>string ereg_replace (string pattern, string replacement, string subject) </li></ul></ul><ul><ul><li>string eregi_replace (string pattern, string replacement, string subject) </li></ul></ul><ul><ul><li>array split (string pattern, string subject [, int limit]) </li></ul></ul><ul><ul><li>array spliti (string pattern, string subject [, int limit]) </li></ul></ul>
  5. 5. Example <ul><li>// POSIX REGEX </li></ul><ul><li>$pattern = &quot;^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+.[a-zA-Z.]{2,5}$&quot;; </li></ul><ul><li>$email = &quot;jack@tamk.com&quot;; </li></ul><ul><li>if ( eregi($pattern, $email) ) </li></ul><ul><li>print &quot;Match&quot;; </li></ul><ul><li>else </li></ul><ul><li>print &quot;Not match&quot;; </li></ul>
  6. 6. Example 2 <ul><li>// POSIX REGEX </li></ul><ul><li>$pattern = &quot;^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+.[a-zA-Z.]{2,5}$&quot;; </li></ul><ul><li>$email = $_GET[&quot;e-mail&quot;]; </li></ul><ul><li>if ( eregi($pattern, $email) ) </li></ul><ul><li>// Save user input to database </li></ul><ul><li>else </li></ul><ul><li>print &quot;Give proper e-mail address!&quot;; </li></ul>
  7. 7. preg function set <ul><li>Perl compatible REGEX: </li></ul><ul><ul><li>int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags [, int $offset ]]] ) </li></ul></ul><ul><ul><li>mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit [, int &$count ]] ) </li></ul></ul><ul><ul><li>array preg_split ( string $pattern , string $subject [, int $limit [, int $flags ]] ) </li></ul></ul>
  8. 8. Example <ul><li>// PERL REGEX </li></ul><ul><li>$pattern = &quot;/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+.[a-zA-Z.]{2,5}$/i&quot;; </li></ul><ul><li>$email = &quot;jack@tamk.com&quot;; </li></ul><ul><li>if ( preg_match($pattern, $email) ) </li></ul><ul><li>print &quot;Match&quot;; </li></ul><ul><li>else </li></ul><ul><li>print &quot;Not match&quot;; </li></ul>
  9. 9. Perl Regex Examples <ul><li>Basic usage: /pattern/modifiers </li></ul><ul><li>Examples: </li></ul><ul><ul><li>/ha jo/ matches &quot;tur ha jo no&quot; </li></ul></ul><ul><ul><li>/ha jo/i matches &quot;tur ha JO NO&quot;; </li></ul></ul>
  10. 10. Some PERL regex rules REGEX Explanation Example . Any single character /a.c/ matches &quot;abc&quot; or &quot;afc&quot; [ ] Single character that is contained within the brackets /[abc]/ matches &quot;a&quot;, or &quot;b&quot; or &quot;c&quot; ^ Find from the beginning of the string /^a/ matches &quot; a ku ankka&quot; $ Find from the end of the string /a$/ matches &quot;aku ankk a &quot; | OR /cat|dog/ matches &quot;dog&quot; or &quot;cat&quot; ^ NOT (when used in middle of pattern) /a^aa/ matches &quot;aba&quot; {n,m} Matches min n, max m /a{1,3}/ matches &quot;a&quot;, or &quot;aa&quot;, or &quot;aaa&quot; * Matches 0-n /a*/ ? Matches 0-1 /(hello)?/ + Matches 1-n /a+/
  11. 11. Examples <ul><li>/.ala/ </li></ul><ul><ul><li>ka vala mpi </li></ul></ul><ul><li>/. Älä/ </li></ul><ul><ul><li>Nyt se loppui . Älä rupea. </li></ul></ul><ul><li>/^begin/ </li></ul><ul><ul><li>begin starts like this. </li></ul></ul><ul><li>/end$/ </li></ul><ul><ul><li>this is the end </li></ul></ul>
  12. 12. Examples <ul><li>/^begin and end$/ </li></ul><ul><ul><li>begin and end </li></ul></ul><ul><li>/one|two|three/ </li></ul><ul><ul><li>this is the one </li></ul></ul><ul><li>/[abc]$/ </li></ul><ul><ul><li>? </li></ul></ul><ul><li>/^Hello my name is ^(Jussi)$/i </li></ul><ul><ul><li>? </li></ul></ul>
  13. 13. More Perl REGEX Rules REGEX Explanation w [a-zA-Z0-9_] W ^ [a-zA-Z0-9_] New Line s Whitespace / Empty S Not whitespaces / Empty d Number D Not Number
  14. 14. Examples <ul><li>/^[+-]?d+.?d*$/ </li></ul><ul><li>/^d{1,2}.d{1,2}.?d{4}?$/ </li></ul><ul><li>/^(d{1,2}):(d{1,2}).(d{1,2})$/ </li></ul><ul><li>/[CDE]:w]+w]+ </li></ul>

×