Regular expressions

From Rosetta Code
Revision as of 01:16, 24 January 2007 by rosettacode>Til
Task
Regular expressions
You are encouraged to solve this task according to the task description, using any language you may know.

The goal of this task is

  • to match a string against a regular expression
  • to substitute part of a string using a regular expression

Perl

Interpreter: perl, v5.8.8

 #!/usr/bin/perl
 $string = "I am a string";
 if ($string =~ /string$/) {
   print "Ends with 'string'\n";
 }
 
 #substitute
 
 $string =~ s/ a / another /  ;
 print $string;