Specifying the String to Match
The Binding operator (=~) is used to "bind" the string to be searched and the pattern.
1 2 $h = "Who's afraid of Virginia Woolf?"; $h =~ /Woo?lf/;
The one line version of the 'if statement' can be combined with a regular expression:
1 2 $h = "Who's afraid of Virginia Woolf?"; print "I'm afraid!\n" if $h =~ /Woo?lf/;
There's also an equivalent "not match" operator !~, which
reverses the sense of the match:
1 2 $h = "Who's afraid of Virginia Woolf?"; print "I'm not afraid!\n" if $h !~ /Woo?lf/;
Matching with a Variable Pattern
You can use a scalar variable for all or part of a regular
expression. For example:
1 2 $pattern = '/usr/local'; print "matches" if $file =~ /^$pattern/;
- Create a script with a regular expression within an if-statement.
- Design the regular express to match an entire sentence, up to the ending period in the provided string.
my $str = "This is a paragraph. A Paragraph is usually made up of more than one sentence.";
- Modify your regular expression to take '.' , '?', and '!; into account as ending punctuation.