preg match si preg_match_all - exemple

 

Expresiile regulate, reprexinta, un tool foarte puternic prin care, pe baza unor reguli, se pot gasi si inlocui, parti din text. Mai jos aveti lista de reguli si ce reprezinta ele si mai multe exemple utile prin care se poate intelege fiecare ce face. 

Regex quick reference

[abc]        A single character: a, b or c
[^abc]      Any single character but a, b, or c
[a-z]         Any single character in the range a-z
[a-zA-Z]   Any single character in the range a-z or A-Z
^      Start of line
$     End of line
A     Start of string
z     End of string
.     Any single character
s     Any whitespace character
S     Any non-whitespace character
d     Any digit
D     Any non-digit
w     Any word character (letter, number, underscore)
W     Any non-word character
b     Any word boundary character
(...)     Capture everything enclosed
(a|b)     a or b
a?     Zero or one of a
a*     Zero or more of a
a+     One or more of a
a{3}     Exactly 3 of a
a{3,}     3 or more of a
a{3,6}     Between 3 and 6 of a
 
Special Sequences
w - matches any single character classified as a “word” character (alphanumeric or “_”)
W - matches any non-“word” character
s - Whitespace (space, tab CRLF)
S - Any non whitepsace character
d - Digits (0-9)
D - Any non digit character
 
Meta Characters
^ - Start of subject (or line in multiline mode)
$ - End of subject (or line in multiline mode)
. - Any character except new line
* - 0 or more times
+ - one or more times; shortest match
| - alternative
() - grouping; "storing"
[] - set of characters
{} - repetition modifier
- quote or special
 
Quantifiers
n* - Zero or more of n
n+ - One or more of n
n? - Zero or one occurrences of n
{n} - n occurrences exactly
{n,} - At least n occurrences
{n,m} - Between n and m occurrences (inclusive)
 
Pattern Modifiers
i - Case Insensitive
m - Multiline mode - ^ and $ match start and end of lines
s - Dotall - . class includes newline
x - Extended– comments and whitespace
e - preg_replace only – enables evaluation of replacement as PHP code
S - Extra analysis of pattern
U - Pattern is ungreedy
u - Pattern is treated as UTF-8
 
Point based assertions
b - Word boundary
B - Not a word boundary
A - Start of subject
Z - End of subject or newline at end
z - End of subject
G - First matching position in subject
 
Assertions
(?=) - Positive look ahead assertion foo(?=bar) matches foo when followed by bar
(?!) - Negative look ahead assertion foo(?!bar) matches foo when not followed by bar
(?<=) - Positive look behind assertion (?<=foo)bar matches bar when preceded by foo
(?<!) - Negative look behind assertion (?<!foo)bar matches bar when not preceded by foo
(? >) - Once-only subpatterns (? >d+)bar Performance enhancing when bar not present
(?(x)) - Conditional subpatterns
(?(3)foo|fu)bar - Matches foo if 3rd subpattern has matched, fu if not
(?#) - Comment (?# Pattern does x y or z)
 
Mai multe exemple pe baza regulilor de mai sus:
 
$string = "What is my ip 127.0.0.1 and my favorite address is http://www.fabbydesign.ro ";

// check if string starts with "ha"
echo preg_match("/^Wh/",$string); // result 1
 
// check if string ends with "ip" - case insensitive
echo preg_match("/ip$/i",$string); // result 0
 
// match for exact string
echo preg_match("/^ip$/i",$string); //result 0
 
// match for empty string
echo preg_match("/^$/i",$string); //result 0
 
// match for string "http://"
echo preg_match("/http:///i",$string); //result 1
 
// search for a string that start's with w and finish with p
echo preg_match("/a.*d/i",$string); //result 1
 
// get domain from url
$string = "http://www.anunturi-utile.ro/detalii_anunt_265902_service_reparatii_turbo_auto.htm link";
preg_match_all( "/(http:\/\/)(www\.)?([a-z0-9-_]+).([a-z]+)/i", $string, $matches );
 
//get email addresses
$string = "If you want to get more info contact me at mail@mail.com and you will recieve more details or at mail@yahoo.com";
preg_match_all( "/([*+!.&$#¦\'\\%\/0-9a-z^_`{}=?~:-]+)@([a-z0-9]+\.)+([a-z0-9]){2,4}/i", $string, $matches );
 
//extract word and digit and insert it into an array with key names setted by me
$string = "foobar: 2008";
preg_match("/(?P<name>\w+): (?P<digit>\d)/",$string,$matches);
 
//get text between square brackets [This] is a [test] string, [eat] my [shorts]
$string = "[This] is a [test] string, [eat] my [shorts]";
preg_match_all("/\[(.*?)\]/",$string,$result);
 
//search and replace
$string = 'The quick brown fox jumped over the lazy dog.';
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements[0] = 'bear';
$replacements[1] = 'black';
$replacements[2] = 'slow';
$stringReturn = preg_replace($patterns, $replacements, $string);
echo $stringReturn; //The bear black slow jumped over the lazy dog.
 
// This example strips excess whitespace from a string
$string = 'foo    o';
$return = preg_replace("/\s+/"," ",$string);
echo $return; // return foo o
 
//reorder data
$string = '100 Rs 50';
$pattern = '/(\d+).(\w+) (\d+)/i';
$replacement = '${1}.$3 $2';
echo preg_replace($pattern, $replacement, $string); //return 100.50 Rs
 
//remove comments from text
$source = "How are you? /* Fine! */ Are you here?";
$return = preg_replace('/(\/\*.*\*\/)/',"", $source);
echo $return; // return How are you?  Are you here?
 
 
 
 

 

0 comentarii

Spune-ti parerea!

Cod validare
Categorii Arhiva
Script de ratingAdaugat pe 16 Noiembrie 2010

PHP PDOAdaugat pe 04 Aprilie 2016

Nu se mai poate, tb sa mai fac si SEOAdaugat pe 30 Mai 2009

preg match si preg_match_all - exempleAdaugat pe 18 Martie 2014

Convertire CDR in PhotoshopAdaugat pe 26 Ianuarie 2011

Azi, 29 martie 2010 am relansat site-ul!Adaugat pe 29 Martie 2010

Version Control Systems (VCS)Adaugat pe 29 Iunie 2015

BulgariaAdaugat pe 04 Mai 2009

Smooth Navigational Menu - meniu pe mai multe niveleAdaugat pe 07 Decembrie 2012

Clasa php pentru editare imagineAdaugat pe 19 Aprilie 2013