Home > Interactive knowledge & Tips n Tricks & other reference stuff > Regular Expression, an introduction with full of examples

Regular Expression, an introduction with full of examples

September 26th, 2010 91 views Leave a comment Go to comments

Let’s search txt files in a folder. If you are in widows OS then you will open search and will type ‘*.txt’. If you are using unix then you’ll use ‘ls *.txt’.

‘*’ is commonly used regular expression’s element. Sometimes we called it as wild character.


Regular expression typography

A regular expression (regex or regexp for short) is a special text string for describing a search pattern.

Take another simplest example.

Amty*.txt

What does above pattern do? It’ll search for all files which are starting with ‘Amty’ and ending with ‘.txt’.

Regular expression is nothing but the combination of such elements along with simple text. Programmers or Mathematics student can understand an element as variable. Now see

       127x+43y=z
       -78x-34y=125z

Above expressions can be written as ‘Ax+By=C’. Here you can put various value of A,B,C to get above result.

Now consider a situation where you have to extract above expression from a page full with some text. For example

Artcle-stack.com is a 127x+43y=z online sharing and learning site.
-78x-34y=125z Users registrations is must to see restricted contents and articles
Subscription is required for email alerts.

You have to extract all the expressions look like ‘Ax+By=C’. Where given pattern is surrounded by space. So a possible RE would be

RE

[\-0-9]*x[\+\-]{1}[0-9]*y=[^ ]*

Result

            [0] => 127x+43y=z
            [1] => -78x-34y=125z

Above RE would not clear to you until you read about elements of Regular expression. So keep reading.

Amit Gupta

Hey! this is Amit Gupta (amty). By profession, I am a Software Eng. And teaching is my passion. Sometimes I am a teacher, as you can see many technical tutorials on my site, sometimes I am a poet, And sometime just a friend of friends...

  1. No comments yet.