Regular expressions: Difference between revisions

m
(→‎{{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";
if ($string =~ /string$/) {
print "Ends with 'string'\n";
}
 
if ($string =!~ "I/^You/) am a string";{
if ($string =~ /string$/) {
print "Ends with 'string'\n";
}
if ($string !~ /^You/) {
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;</lang>
$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";
 
if ($string =~ "Is/\bam\b/was/) am{ # \b is a string";word border
if ($string =~ s/\bam\b/was/) { # \b is a word border
print "I was able to find and replace 'am' with 'was'\n";
}</lang>
}
 
Options
<lang perl># 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)
 
$string =~ s/ a i/ another u/ig; # makeswould change "I am a string" into "Iu am anothera stringstrung"</lang>
# 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)
$string =~ s/i/u/ig; # would change "I am a string" into "u am a strung"
 
=={{header|PHP}}==
Anonymous user