Wiki: Regular Expressions


Note: You are viewing an old revision of this page. View the current version.


If you've ever typed "cp *.html ../" at the UNIX command prompt, or entered "garden?" into a web-based search engine, you've already used a simple regular expression. Regular expressions ("regex's" for short) are sets of symbols and syntactic elements used to match patterns of text.

Even these simple examples testify to the power of regular expressions. In the first instance, you've copied all the files which end in ".html" (as opposed to copying them one by one); in the second, you've conducted a search not only for "garden," but for "garden, gardening, gardens, and gardeners" all at once.

For a tool with full regex support, metacharacters like "*" and "?" (or "wildcard operators," as they are sometimes called) are only the tip of the iceberg. Using a good regex engine and a well-crafted regular expression, one can easily search through a text file (or a hundred text files) searching for words that have the suffix ".html" (but only if the word begins with a capital letter and occurs at the beginning of the line), replace the .html suffix with a .sgml suffix, and then change all the lower case characters to upper case. With the right tools, this series of regular expressions would do just that:

<pre>
    s/(^[A_Z]{1})([a-z]+)\.sgml/\1\2\.html/g
    tr/a-z/A-Z/
</pre>

As you might guess from this example, concision is everything when it comes to crafting regular expressions, and while this syntax won't win any beauty prizes, it follows a logical format which you can learn to read and write easily with just a little bit of practice.

Unfortunately, regex syntax and idiosyncracies vary a lot between applications. Even if a regex language looks the same in two different programs, chances are it behaves differently under certain circumstances.

http://www.amwaypower.com/amwaypower/ http://www.amwaypower.com/amway_china/ http://www.amwaypower.com/amway_gongsi/ http://www.amwaypower.com/amway_chanpin/ http://www.amwaypower.com/amway_niucuilai/ http://www.amwaypower.com/amway_usa/ http://www.amwaypower.com/amway_zhixiao/ http://www.paiwei.cn/add/ http://www.playseo.com/bjlvyou/ http://www.playseo.com/zuche021/ http://www.playseo.com/zuche8/ http://www.playseo.com/zuche/ http://www.saishuo.com/tiezhixiangji/ http://www.sunflow.com.cn/sunflow/ http://www.txhf8.com/add/ http://diandu123.51.net/ http://usauniversity.51.net/ http://pagerank.51.net/ http://amwaypower.51.net/ http://amwaypower.51.net/amway_china/ http://amwaypower.51.net/amway_gongsi/ http://amwaypower.51.net/amway_chanpin/ http://amwaypower.51.net/ay_niucuilai/ http://amwaypower.51.net/amway_usa/ http://amwaypower.51.net/amway_zhixiao/ http://fadianjizu.51.net/ http://bjlvyou.51.net/index.htm http://zuche021.51.net/ http://zuche8.51.net/ http://tiezhixiangji.51.net/ http://sunflow.51.net/ http://zhibanji.51.net/ http://siweiyuan15.51.net/ http://siweiyuan14.51.net/ http://siweiyuan13.51.net/ http://siweiyuan12.51.net/ http://siweiyuan11.51.net/ http://siweiyuan10.51.net/ http://siweiyuan09.51.net/ http://siweiyuan08.51.net/ http://siweiyuan07.51.net/ http://siweiyuan06.51.net/ http://siweiyuan05.51.net/ http://siweiyuan04.51.net/ http://siweiyuan03.51.net/ http://siweiyuan02.51.net/ http://siweiyuan01.51.net/ http://siweiyuan.51.net/

http://www.playseo.com/somewebpage.htm http://www.souwho.com/somewebpage.htm http://pagerank.51.net/somewebpage.htm http://www.amwaypower.com/keywords/free1.htm http://www.boshilw.com/keywords/free1.htm http://www.dfhb.com/keywords/free1.htm http://www.dxslw.com/keywords/free1.htm http://www.emicn.com/keywords/free1.htm http://www.gfqy.com/keywords/free1.htm http://www.google123.net/keywords/free1.htm http://www.google123.org/keywords/free1.htm http://www.lnhbsb.com/keywords/free1.htm http://www.ly-yufeng.com/keywords/free1.htm http://www.lyzhuguang.com/keywords/free1.htm http://www.lxep.com/keywords/free1.htm http://www.newstyle-w.com/keywords/free1.htm http://www.paiwei.cn/keywords/free1.htm http://www.playseo.com/keywords/free1.htm http://www.saishuo.com/keywords/free1.htm http://www.sh-xinping.com/keywords/free1.htm http://www.shhcy.com/keywords/free1.htm http://www.songshangroup.com/free1.htm http://www.souwho.com/keywords/free1.htm http://www.sunflow.com.cn/keywords/free1.htm http://www.suntront.com.cn/keywords/free1.htm http://www.think-t.com/keywords/free1.htm http://www.txhf8.com/keywords/free1.htm http://www.vita-biotech.com/keywords/free1.htm http://www.writerlw.com/keywords/free1.htm http://www.xinpushihua.com/keywords/free1.htm http://www.xtzn.com/keywords/free1.htm http://www.ycc-zipper.com.cn/keywords/free1.htm http://pagerank.51.net/free1.htm


Sponsored by: Labrat Technology -- Need Consulting Services? Programming, Maintenance, Security, Database Analysis & Design, Hosting -- we do it all!
Copyright 2005 Robert Melton
Send questions and comments to
Last updated March 10th, 2005