Regular Expressions



The three:
m// match (m not required) 
tr// translate 
s// substitutes
"Meta characters" and their meaning
\ escapes any character in a regular expression. 
^ match at the begining 
$ match at the end of the string (line if /m) 
| logical or
Quantifiers
. Single character 
? Match 0 or 1 times. 
* match 0 or more times. 
+ match 1 or more times.
Operators
c Do not reset search position on a failed match when /g is in effect. 
g Match globally, i.e., find all occurrences. 
i Do case-insensitive pattern matching. 
m Treat string as multiple lines. 
o Compile pattern only once. 
s Treat string as single line. 
x Use extended regular expressions.
Basic examples of matching
if ($text ~= /string/){#The varible $text contains the word 'string'.}
Would execute if the varible $text contained
    'This is a string of text'
    'The phaser left a blastring'

but not
    'String is an important part...'

For this last example to match we add an i (ignore case).

if ($text ~= /string/i){#The varible $text contains the word 'string'.}

You can also test to see if a pattern doesn't match a string with
if ($text !~ /string/){#Code for negate goes here;}

So here are some common, helpful regexp examples.

Return to Perl Help


[Our portfolio] [A history of Computers and Networks]
[Perl help] [ABAP help] [MySQL help]
[TCP/IP troubleshooting] [HTML help] [Feedback] [Humor]
Advertise on Golden Ink's Georgia Network


In Association with Amazon.com