MyPage is a personalized page based on your interests.The page is customized to help you to find content that matters you the most.


I'm not curious

Few commonly used regular expressions

Published on 02 November 12
2084
0
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);
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);
This review is listed under Development & Implementations Community

Related Posts:
Post a Comment

Please notify me the replies via email.

Important:
  • We hope the conversations that take place on MyTechLogy.com will be constructive and thought-provoking.
  • To ensure the quality of the discussion, our moderators may review/edit the comments for clarity and relevance.
  • Comments that are promotional, mean-spirited, or off-topic may be deleted per the moderators' judgment.
Awards & Accolades for MyTechLogy
Winner of
REDHERRING
Top 100 Asia
Finalist at SiTF Awards 2014 under the category Best Social & Community Product
Finalist at HR Vendor of the Year 2015 Awards under the category Best Learning Management System
Finalist at HR Vendor of the Year 2015 Awards under the category Best Talent Management Software
Hidden Image Url