Escolar Documentos
Profissional Documentos
Cultura Documentos
Ajax
PHP regular expressions examples
ASP
ASP.NET
Keep your snippets safe! Read about our program and find out why you should use roScripts to save your code snippets.
Actionscript
C and C++
CFML GET THE FEED
Do you like our posts? Wanna stay informed and be the
CGI and Perl first to know when we publish new content?
CSS
C#
SUBSCRIBE TO OUR NEWSLETTER
Flash Weekly newsletters with the best websites organized by
HTML categories and other news.
Java
JavaScript The regular expression, as a pattern, can match all
MySQL
kinds of text strings helping your application validate,
compare, compute, decide etc. It can do simple or very
PHP
complex string manipulations. The list of possibilities is
Photoshop
enormous when it comes to what you can achieve
SEO using regular expressions. You can take any phrase
VB that starts with an "A" or any character and do various
XML things with it. You can match phone numbers, email
Websites addresses, url's, credit card numbers, social security
Other numbers, zip codes, states, cities.....(the list never
ends). A huge script that is supposed to validate a user
input and prepare it for sql can be reduced to only one line with the help of preg_replace.
The next function will get the part of a given string ($text) matched by the regex ($regex) using a group srorage
($regs). By changing the $regs[0] to $regs[1] we can use a capturing group (in this case griup 1) to match against.
20%capturing
The Discounts on Hosting
group can also have a name ($regs['groupname']):
Quality Web Hosting Got Cheaper 350 Gigs Space 3 TB
space $4.77/mo
function do_reg($text, $regex, $regs)
www.huge-discount-on-hosting.com
{
if (preg_match($regex, $text, $regs)) {
$result = $regs[0];
}
file:///D:/Sourcecodes/Web/PHP/amp%20stack%20works/TigerIT-Firefly/zend%20on%20Kausar/shoeb%20vi_up/ZCE/string/Regular%20expression/ 1/11
8/20/2017 PHP regular expressions examples
$result = $regs[0];
}
else {
Your Ad Here $result = "";
}
return $result;
}
The following function will return an array of all regex matches in a given string ($text):
Next we can iterate (loop) over all matches in a string ($text) and output the results:
Extending the above one we can iterate over all matches ($text) and capture groups in a string ($text):
Columns
Credit cards
CSV
Dates
//Date dd/mm/yyyy
//01/01/1900 through 31/12/2099
//Matches invalid dates such as February 31st
'(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)[0-9]{2}'
//Date mm/dd/yyyy
//01/01/1900 through 12/31/2099
//Matches invalid dates such as February 31st
'(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[0-9]{2}'
//Date yyyy-mm-dd
//1900-01-01 through 2099-12-31
//Matches invalid dates such as February 31st
'(19|20)[0-9]{2}[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])'
Delimiters
Email addresses
//Email address
//Use this version to seek out email addresses in random documents and texts.
//Does not match email addresses using an IP address instead of a domain name.
//Does not match email addresses on new-fangled top-level domains with more than 4 letters such as .museum.
//Including these increases the risk of false positives when applying the regex to random documents.
'\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b'
HTML
//HTML comment
'<!--.*?-->'
//HTML file
//Matches a complete HTML file. Place round brackets around the .*? parts you want to extract from the file.
//Performance will be terrible on HTML files that miss some of the tags
//(and thus won't be matched by this regular expression). Use the atomic version instead when your search
//includes such files (the atomic version will also fail invalid files, but much faster).
'<html>.*?<head>.*?<title>.*?</title>.*?</head>.*?<body[^>]*>.*?</body>.*?</html>'
//HTML tag
//Matches the opening and closing pair of whichever HTML tag comes next.
//The name of the tag is stored into the first capturing group.
//The text between the tags is stored into the second capturing group.
'<([A-Z][A-Z0-9]*)[^>]*>(.*?)</\1>'
//HTML tag
//Matches the opening and closing pair of a specific HTML tag.
file:///D:/Sourcecodes/Web/PHP/amp%20stack%20works/TigerIT-Firefly/zend%20on%20Kausar/shoeb%20vi_up/ZCE/string/Regular%20expression/ 4/11
8/20/2017 PHP regular expressions examples
//Matches the opening and closing pair of a specific HTML tag.
//Anything between the tags is stored into the first capturing group.
//Does NOT properly match tags nested inside themselves.
'<%TAG%[^>]*>(.*?)</%TAG%>'
//HTML tag
//Matches any opening or closing HTML tag, without its contents.
'</?[a-z][a-z0-9]*[^<>]*>'
IP addresses
//IP address
//Matches 0.0.0.0 through 999.999.999.999
//Use this fast and simple regex if you know the data does not contain invalid IP addresses.
'\b([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\b'
//IP address
//Matches 0.0.0.0 through 999.999.999.999
//Use this fast and simple regex if you know the data does not contain invalid IP addresses,
//and you don't need access to the individual IP numbers.
'\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b'
//IP address
//Matches 0.0.0.0 through 255.255.255.255
//Use this regex to match IP numbers with accurracy, without access to the individual IP numbers.
'\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b'
//IP address
//Matches 0.0.0.0 through 255.255.255.255
//Use this regex to match IP numbers with accurracy.
//Each of the 4 numbers is stored into a capturing group, so you can access them for further processing.
'\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9
Lines
Logs
file:///D:/Sourcecodes/Web/PHP/amp%20stack%20works/TigerIT-Firefly/zend%20on%20Kausar/shoeb%20vi_up/ZCE/string/Regular%20expression/ 5/11
8/20/2017 PHP regular expressions examples
Numbers
//Number: integer
//Will match 123 and 456 as separate integer numbers in 123.456
'\b\d+\b'
//Number: integer
//Does not match numbers like 123.456
'(?<!\S)\d++(?!\S)'
Passwords
//Password complexity
//Tests if the input consists of 6 or more letters, digits, underscores and hyphens.
//The input must contain at least one upper case letter, one lower case letter and one digit.
'\A(?=[-_a-zA-Z0-9]*?[A-Z])(?=[-_a-zA-Z0-9]*?[a-z])(?=[-_a-zA-Z0-9]*?[0-9])[-_a-zA-Z0-9]{6,}\z'
//Password complexity
//Tests if the input consists of 6 or more characters.
//The input must contain at least one upper case letter, one lower case letter and one digit.
'\A(?=[-_a-zA-Z0-9]*?[A-Z])(?=[-_a-zA-Z0-9]*?[a-z])(?=[-_a-zA-Z0-9]*?[0-9])\S{6,}\z'
File paths
//Path: Windows
'\b[a-z]:\\[^/:*?"<>|\r\n]*'
//Path: Windows
//Different elements of the path are captured into backreferences.
'\b((?#drive)[a-z]):\\((?#folder)[^/:*?"<>|\r\n]*\\)?((?#file)[^\\/:*?"<>|\r\n]*)'
Phone numbers
Postal codes
Programming
//Programming: # comment
//Single-line comment started by # anywhere on the line
'#.*$'
//Programming: /* comment */
//Does not match nested comments. Most languages, including C, Java, C#, etc.
//do not allow comments to be nested. I.e. the first */ closes the comment.
'/\*.*?\*/'
//Programming: // comment
//Single-line comment started by // anywhere on the line
'//.*$'
//Programming: GUID
//Microsoft-style GUID, numbers only.
'[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}'
//Programming: GUID
//Microsoft-style GUID, with optional parentheses or braces.
//(Long version, if your regex flavor doesn't support conditionals.)
'[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}|\([A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[
//Programming: GUID
//Microsoft-style GUID, with optional parentheses or braces.
//Short version, illustrating the use of regex conditionals. Not all regex flavors support conditionals.
//Also, when applied to large chunks of data, the regex using conditionals will likely be slower
//than the long version. Straight alternation is much easier to optimize for a regex engine.
'(?:(\()|(\{))?[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}(?(1)\))(?(2)\})'
//Programming: String
//Quotes may appear in the string when escaped with a backslash.
//The string may span multiple lines.
'"[^"\\]*(?:\\.[^"\\]*)*"'
//Programming: String
//Quotes may appear in the string when escaped with a backslash.
//The string cannot span multiple lines.
'"[^"\\\r\n]*(?:\\.[^"\\\r\n]*)*"'
//Programming: String
//Quotes may not appear in the string. The string cannot span multiple lines.
'"[^"\r\n]*"'
file:///D:/Sourcecodes/Web/PHP/amp%20stack%20works/TigerIT-Firefly/zend%20on%20Kausar/shoeb%20vi_up/ZCE/string/Regular%20expression/ 7/11
8/20/2017 PHP regular expressions examples
Quotes
//Quotes: Replace smart single quotes and apostrophes with straight single quotes.
//Unicode version for use with Unicode regex engines.
preg_replace("[\u2018\u2019\u201A\u201B\u2032\u2035]", "'", $text);
//Quotes: Replace smart single quotes and apostrophes with straight single quotes.
//ANSI version for use with 8-bit regex engines and the Windows code page 1252.
preg_replace("[\x82\x91\x92]", "'", $text);
Escape
Security
//Security: Unicode code and unassigned characters excl. tab and CRLF
//Matches any single non-printable code character that may cause trouble in certain situations.
//Also matches any Unicode code point that is unused in the current Unicode standard,
//and thus should not occur in text as it cannot be displayed.
//Excludes tabs and line breaks.
'[^\P{C}\t\r\n]'
//Security: Unicode code and unassigned characters incl. tab and CRLF
//Matches any single non-printable code character that may cause trouble in certain situations.
//Also matches any Unicode code point that is unused in the current Unicode standard,
//and thus should not occur in text as it cannot be displayed.
//Includes tabs and line breaks.
'\p{C}'
Trim
//Trim whitespace (including line breaks) at the start and the end of the string
preg_replace("\A\s+|\s+\z", "", $text);
URL's
Words
XML to PDF
Powerful XML to PDF formatting Supports XSL-FO V1.1
www.antennahouse.com
file:///D:/Sourcecodes/Web/PHP/amp%20stack%20works/TigerIT-Firefly/zend%20on%20Kausar/shoeb%20vi_up/ZCE/string/Regular%20expression 10/11
8/20/2017 PHP regular expressions examples
PHP Articles Other
View all
Comments
For coding help and further support please use the forum.
x.mountain says:
thanks, I spent too much time working on this same thing. I should have posted it, but you beat me to the
punch.
2007-07-09 | 02:07 am
mrwilson says:
You are right Vincent, the worst part is with all this invaluable information here, I can think of much more I
wish was here! This is an excellent website by someone who cares.
2007-05-20 | 06:12 am
Vincent says:
You know what? This may be the best thing since sliced bread. It's crazy the amount of time people spend
making these things when they're already right infront of them... thanks!!
2007-05-20 | 01:27 am
file:///D:/Sourcecodes/Web/PHP/amp%20stack%20works/TigerIT-Firefly/zend%20on%20Kausar/shoeb%20vi_up/ZCE/string/Regular%20expression 11/11