Anonymous user
Regular expressions: Difference between revisions
m
→{{header|Perl}}: formatting
(→{{header|Tcl}}: demonstrate substring extraction as well, and apply a bunch of corrections) |
m (→{{header|Perl}}: formatting) |
||
Line 361:
{{works with|Perl|5.8.8}}
Test
<lang perl>$string = "I am a string";
print "Ends with 'string'\n";▼
▲ if ($string =~ /string$/) {
▲ print "Ends with 'string'\n";
▲ }
print "Does not start with 'You'\n";
}</lang>
Substitute
<lang perl>$string = "I am a string";
$string =~ s/ a / another /; # makes "I am a string" into "I am another string"▼
▲ print $string;
Test and Substitute
<lang perl>$string = "I am a string";
print "I was able to find and replace 'am' with 'was'\n";
}</lang>
Options
▲
▲ # add the following just after the last / for additional control
▲ # g = globally (match as many as possible)
▲ # i = case-insensitive
▲ # s = treat all of $string as a single line (in case you have line breaks in the content)
▲ # m = multi-line (the expression is run on each line individually)
=={{header|PHP}}==
|