Documente Academic
Documente Profesional
Documente Cultură
http://arraylist.blogspot.com
Regular expressions
We usually talk about regular expressions and
pattern matching within the context of scripting
language such as Perl or Shell script.
http://arraylist.blogspot.com
Regular expressions
Pattern matching in Perl occurs using a match
operator such as
m// or m:: or m,,
Example – m/simple/
http://arraylist.blogspot.com
Regular expressions
Metacharacters have to be preceded with a \
during pattern matching.
Metacharacters ^ $ ( ) \ | @ [ { ? . + *
http://arraylist.blogspot.com
Regular expressions
m// if we use // as delimiters – we can avoid
the character m during pattern matching.
So m/simple/ can be /simple/
http://arraylist.blogspot.com
Regular expressions
Metacharacters . ^ $ ( ) \ | @ [ { ? + *
http://arraylist.blogspot.com
Regular expressions
Metacharacters . ^ $ ( ) \ | @ [ { ? + *
Special characters
\n – newline
\r – carriage return
\t – tab
\f – formfeed
http://arraylist.blogspot.com
Regular expressions
Quantifiers – tells the regex , how many times a
pattern should be matched.
“+” match minimum once or as many times as it
occurs
Example /go+d/ matches good but not god
“*” matches preceding character 0 or more times
http://arraylist.blogspot.com
Regular expressions
{} matched characters specified number of times
/a{5,10}/ - matches the character a at least 5
times ,
but no more than 10 times
/a{5,}/ - matches 5 and more times.
/a{0,2}/ - matches 0 or at the most 2 times.
/a{5}/ - match exactly six times
http://arraylist.blogspot.com
Regular expressions
Square brackets [] and character class
http://arraylist.blogspot.com
Regular expressions
Special Character classes
http://arraylist.blogspot.com
Regular expressions
Alternation and Anchors
Alternation uses | which means “or”
Eg. /tea|coffee/ check if string contains tea or
coffee
Grouping with alternation
Eg. /(fr|bl|cl)og/ if string contains frog or blog
or clog
http://arraylist.blogspot.com
Regular expressions
Substitution
Syntax – s///
s/searchstring/replacementstring/
Eg. $_ = “lies does not make sense”
s/lies/truth/ “truth does not make sense”
http://arraylist.blogspot.com