Xxaxx's Xperimints #9
More on 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:
|