Newbie dot Org HomePage
Visit one of our web buddies
Xxaxx's Xperimints #8
Regular Expressions
Playing with PERL

Let's play around with regular expressions shall we?

In Perl you can use regular expressions and the substitution function to transform a string:

$somestring =~ s/old/new/g;

$somestring -- is the string to be acted upon. A string is a bunch of character "strung" together likes beads on a "string". Hence the name. The sentence is "string" made up of individual letters "characters".

=~ -- a strange function which translate into something like "let the expression on the left be modified by the equation on the right and become that transformed thing". Obviously that is quite a mouthful so those crazy unix type dude use "=~".

s -- The s operator searches the string passed to it for items matching the regular expression defined by old and then replaces all instances with new.

old -- the regular expression that "s" looks for.

new -- the stuff to substitute wherever "old" was found.

g -- an option flag telling "s" to do the operation on the whole string.

; -- the infamous semi-colon that we still forget sometimes.

So let's try this:

$string = ("");

$string =~ s///g;

Warning: If you leave any trailing spaces on your "old" expressions they will not work.
"[0-9] " will not work. But "[0-9]" will work. So don't leave spaces around.

We'd like to thank the good folks at UserActive.Com.
Their great tutorials have helped in many areas.

Clod Monay User Active Mascot


We have included in the text below many of the hints that will show up (one at a time) while you are trying your hand at regular expressions. Every time you try and expression a different (random) clue will be printed. Here you can read all the clues.


Try [aeiou].
See what that does:


Look for a combination of letters like "poop" rather than just a single character.


If you want to capture CAPITAL letter too you can try [aeiouAEIOU].


What if you want to add the "new" where the "old" was found rather than replace it?

To make that work you need to remember the "old" so that it can be stuffed back in along with the "new".

There is a very specific way of doing that. It's like a magic incantation. Meaning just do it until it makes sense.

Here's the deal: put "(" and ")" around the "old" as in ([aeiou]) then use $1 along with the "new".

For example you can try:

$somestringg =~ s/([aeiou])/X$1/g;


As given in a different clue [aeiouAEIOU] will matche any lower or Upper case vowel.

Now here is a tricky bit (those PERL folks are really sneaky some times). If you want to match everything except the vowels you can use the expression [^aeiouAEIOU]. This will match all the vowels NOT.

It's the fault of that ^ thingy. It means take whatever is mentioned next and do the opposite. Tricky huh?


To save typing you can do stuff like [0-9] to match any single digit from 0 to 9.

Or, once again to be tricky you could try [^0-9] to match any non single digit. It's that tricky "^" NOT thing again.


How about trying [a-z] or [a-zA-Z]?

You will find that these will match any lowercase letter in the first case and matches any lower OR upper case letter in the second case.


If you want to match any digit or letter you can do [a-zA-Z0-9].

And, of course, [^a-zA-Z0-9] will match anything except digits and letters.

But don't take our word for it. TRY IT!


To make stuff even trickier (and easier on the typing) PERL has some pre-defined character classes:

\d -- is equivalent to [0-9]

\w -- is equivalent to [a-zA-Z0-9]

\s -- matches any space character.

Now one might (if they were being excessively logical) think that ^\d would be NOT any digits. That would be wrong. Check out the following:

\D -- matches non digits (i.e. digits-NOT!)

\W -- stuff other than letters & digits (i.e. letters and digits-NOT!)

\S -- anything other than spaces (i.e. spaces-NOT!)