How To Say "Ummmm...." In Three Different Languages - Administration
A pattern-matching command in Perl usually looks like this:
Let's take a closer look at each of these components.
The operator can either be an "m" or an "s", depending on the purpose of the regular expression -"m" is used for "match" operations only, while "s" is used for "substitution" operations.
The regular expression is the pattern that is to be matched. This pattern can be constructed using a variety of characters, meta-characters and pattern anchors.
The string to replace is...well, the string to be replaced in a find-and-replace operation. Yeah, every once in a while, we slip you an easy one.
Finally, the modifiers are used to control the manner in which a particular regex is applied. There are a whole bunch of modifiers, some of them with pretty exotic names; unfortunately, none of them are single, or interested in going out to dinner with you.
So, the statement
would replace the first occurrence of the word "love" with "lust". And if you
wanted to perform a global search-and-replace operation, you'd use the "g" modifier, like this
And they say romance is dead!
You can also use case-insensitive pattern matching - simply add the "i" modifier, as in the following example, and watch in awe as Perl matches "jewel", "Jewel" and "JEWEL".
In Perl, all interaction with regular expressions takes place via an equality
operator, represented by =~; this is used as follows.
$flag =~ m/abc/
$flag returns true if $flag contains "abc"
$flag =~ s/abc/ABC/
replaces abc in the variable $flag with ABC And here's an example of a simple
Perl program which asks for your email address, and compares it with a regex to verify whether or not it's in the correct format.
# get input
print "So what's your email address, anyway?n";
# match and display result
if($email =~ /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/)
print("Ummmmm....that sounds good!n");
print("Hey - who do you think you're kidding?n");
As you can see, the most important part of this program is the regular expression
- it's been dissected below:
matches the username part of the email address - this could be either a number,
a character, or a combination of both.
This is followed by an @ symbol, which is followed by the domain part of the address; this could again include letters or numbers, and uses a period as a delimiter - not our usage of an "escaped" period and the "+" meta-character to represent these conditions in the second half of the expression
Obviously, this is simply an illustrative example - if you're planning to use
it on your Web site, you need to refine it a bit. For example, the script above won't accept email addresses of the form email@example.com - although such addresses are also pretty common on the Web. You have been warned!
If you prefer PHP to Perl, you need to use the ereg() function for all pattern matching operations,this usually takes the format
where "pattern" is the pattern to be matched, and "string" is the character string
to be searched for the pattern. The next example should illustrate this a little more clearly:
"Ummmmm....that sounds good!";
echo "Hey - who do you think you're kidding?";
which can be used to match patterns in strings and variables. The important thing here is the test() method, which searches for a pattern in a string or variable, and returns either true or false - its illustrated in the example below.
<!-- start hiding
// obtain form value into variable
var email = obj.email.value;
var pattern = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
test for pattern
flag = pattern.test(email);
alert("Hey - who do you think you're
// stop hiding -->
<input name="email" type="text">
Obviously, there's a whole lot more that you can do with regular expressions
- checking email addresses is just the tip of the iceberg. You can use regular expressions to validate phone numbers, currency figures, Web site URLs, and a whole lot more - all you need is a little bit of creativity and patience, a few slices of leftover pizza...and a therapist who cares.