Common Operators
Here, you will learn how to use regular expressions as a main modifier tool in Perl.
Regular expressions are the main search/modifier tool in perl. Here are some of the modifier you can use for your searches.
“ . “ : “.” Will match any ONE character in a regular expression.
example :
“I have . children” would match “I have 2 children” “I have 3 children” “I have 4 children” but would not match “I have 10 children” because “.” Can only replace ONE character.
Also “.” Cannot replace the null character.
“hel.o” would not match “helo”
“ ?“ : this metacharacter is used to tell Perl to search for matches with or without the character immediately before the “?”
Example:
T?ime would match “Time” or “ime”
“*” : Any character before the “*” can be repeated any number of time.
Example:
“t*ime” would match “ime” time” “tttime” “ttttttttttttttttttttttttime”.
“+” : same as “*” except that the + implies that the character must be present at least once.
Example:
“t*ime” would match “time” “tttime” “ttttttttttttttttttttttttime” but would not match “ime”.
“.*” while being a combination of 2 meta character are widly used because they allow Perl to look for broad matches.
Example:
“t.*e” would match “te” “time” “trace” …etc…any string beginning by “t” and finishing by “e”.
There is simply not enough special character to assign to special function.To paliat that the “\” character is used to turn regular character into metaharacter.
\d will look for all DIGITAL characters.
Exemple
“time \d” would match “time 5” but would not match “time 54” or “time e”
\D will look for all NON DIGITAL characters.
Exemple
“time \D” would match “time e” but would not match “time 5”
\w will look for a number , letter or “_”
Exemple
“time \w” would match “time 5” or “time e” but would not match “time &”
\W
Exemple
“time \w” would not match “time 5” or “time e” but would match “time &” or “time +”
\s is the special character for space.
Exemple
“more\stime” would match “more time” but would not match “moretime” or “more time”
\S
matches any non-whitespace character
\b
“word” boundary
\B
not a “word” boundary
{number,number} : while the “*” metacharacter will allow a match with any umber of occurance this one allows you to specify a range of occurancces.
Example :
T{2,4}ime would match “tiime”,”tiiime”,”tiiiime” nut not “time” ot “tiiiiime”
“|” mean either or.
Example:
Time|again would match any string containing time or again or both.