Regular expressions: Difference between revisions
Content added Content deleted
(→{{header|Tcl}}: demonstrate substring extraction as well, and apply a bunch of corrections) |
m (→{{header|Perl}}: formatting) |
||
Line 361: | Line 361: | ||
{{works with|Perl|5.8.8}} |
{{works with|Perl|5.8.8}} |
||
Test |
Test |
||
<lang perl>$string = "I am a string"; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
if ($string !~ /^You/) { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
if ($string !~ /^You/) { |
|||
print "Does not start with 'You'\n"; |
print "Does not start with 'You'\n"; |
||
}</lang> |
|||
} |
|||
Substitute |
Substitute |
||
<lang perl>$string = "I am a string"; |
|||
$string =~ s/ a / another /; # makes "I am a string" into "I am another string" |
|||
⚫ | |||
⚫ | |||
⚫ | |||
Test and Substitute |
Test and Substitute |
||
<lang perl>$string = "I am a string"; |
|||
if ($string =~ s/\bam\b/was/) { # \b is a 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"; |
print "I was able to find and replace 'am' with 'was'\n"; |
||
}</lang> |
|||
} |
|||
Options |
Options |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
$string =~ s/i/u/ig; # would change "I am a string" into "u am a strung" |
|||
=={{header|PHP}}== |
=={{header|PHP}}== |