Perl Regular Expressions

Regular expressions isn’t where my biggest talents lies, but here I collect small things encountered over time.

The expression is build up like this:
action/ matching expression / what to substitue it with / behaviour

Action
s/ –> substitutes
m/ –> matches an expression to return true or false.

Behaviour
/g –> matches all entries (globally)

Removes all white spaces in the string
my $test = ” +47 22 22 22 22 “;
$test =~ s/\s//g;
print $test; # prints out +4722222222

Match a + sign at the beginning of the string
if ( $test =~ /^\+/ ) {
print “Svar:” .$test .”\n”;
} else {
print “Matcher ikke\n”;
}

# See if the phone number starts with a country code ( a ‘+’ sign or ’00′)
if ( $test =~ /^\+|00/ ) {
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>