Regular expressions: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
 
No edit summary
Line 1: Line 1:
{{task}}
{{task}}
{{Regular expression matching}}


The goal of this task is
The goal of this task is
* to match a string against a regular expression
* to match a string against a regular expression
* to substitute part of a string using 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;

Revision as of 01:16, 24 January 2007

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;