Few commonly used regular expressions
Published on 02 November 12
2084
0
Here are few very commonly used regular expressions. This example is in PHP
// find email address $str = "asdf fasf afdd d pawan269@gmail.com adfsdf asdf"; $find = "/[a-zA-Z0-9._]+@[a-zA-Z0-9]+.[a-zA-Z]{2,5}/"; preg_match_all($find, $str, $matches); print_r($matches); // find all domain names $str = "asfasd fasdf asdfas dfa http://google.com sdf www.GOOGLE.com asd fasd http://www.google.com fasd fdfdf "; $find = "@[http://|www.]+[a-zA-Z0-9]+.[a-zA-Z]{2,5}@"; preg_match_all($find, $str, $matches); print_r($matches); // will take out last 4 chars $str = "SG-7-ABCD"; $find = "/[a-zA-Z]{4}$/"; preg_match_all($find, $str, $matches); print_r($matches); // will take out first 3 chars $str = "xyz-23"; $find = "/^[a-zA-Z]{3}/"; preg_match_all($find, $str, $matches); print_r($matches); // find all hex color code in a string $str = "asdfas fa #FF22FF adsf #DFDFDF dsfasf #123456 afsd f"; $find = "/[#0-9a-fA-F]{7}/"; preg_match_all($find, $str, $matches); print_r($matches); // find date (mysql format) in string $str = "asdf fasf afdd d 2010-04-12 17:20:25 adfsdf asdf"; $find = "/[0-9]{4}+-[0-9]{2}+-[0-9]{2} [0-9]{2}+:[0-9]{2}+:[0-9]{2}/"; preg_match_all($find, $str, $matches); print_r($matches);
// find email address $str = "asdf fasf afdd d pawan269@gmail.com adfsdf asdf"; $find = "/[a-zA-Z0-9._]+@[a-zA-Z0-9]+.[a-zA-Z]{2,5}/"; preg_match_all($find, $str, $matches); print_r($matches); // find all domain names $str = "asfasd fasdf asdfas dfa http://google.com sdf www.GOOGLE.com asd fasd http://www.google.com fasd fdfdf "; $find = "@[http://|www.]+[a-zA-Z0-9]+.[a-zA-Z]{2,5}@"; preg_match_all($find, $str, $matches); print_r($matches); // will take out last 4 chars $str = "SG-7-ABCD"; $find = "/[a-zA-Z]{4}$/"; preg_match_all($find, $str, $matches); print_r($matches); // will take out first 3 chars $str = "xyz-23"; $find = "/^[a-zA-Z]{3}/"; preg_match_all($find, $str, $matches); print_r($matches); // find all hex color code in a string $str = "asdfas fa #FF22FF adsf #DFDFDF dsfasf #123456 afsd f"; $find = "/[#0-9a-fA-F]{7}/"; preg_match_all($find, $str, $matches); print_r($matches); // find date (mysql format) in string $str = "asdf fasf afdd d 2010-04-12 17:20:25 adfsdf asdf"; $find = "/[0-9]{4}+-[0-9]{2}+-[0-9]{2} [0-9]{2}+:[0-9]{2}+:[0-9]{2}/"; preg_match_all($find, $str, $matches); print_r($matches);
This review is listed under
Development & Implementations
Community
Related Posts:
Post a Comment