Anonymous user
Regular expressions: Difference between revisions
→[[Perl]]
No edit summary |
|||
Line 10:
'''Interpreter:''' perl, v5.8.8
Test
$string = "I am a string";▼
if ($string =~ /string$/) {
}
if ($string !~ /^You/) {
print "Does not start with 'You'\n";
}
Substitute
$string =~ s/ a / another /; # makes "I am a string" into "I am another string"
Test and Substitute
$string = "I am a string";
if ($string =~ s/\bam\b/was/) { # \b is a word border
print "I was able to find and replace 'am' with 'was'\n";
}
Options
# add the following just after the / for additional control
# g = globaly (match as many as possible)
# i = case-insensitive
# s = treat all of $string as a single line (incase you have line breaks in the content)
# m = multi-line (the expression is run on each line individually)
▲ print $string;
|