Regular expressions: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
m (Changed over to headers)
Line 5: Line 5:
* to substitute part of a string using a regular expression
* to substitute part of a string using a regular expression


==[[AppleScript]]==
=={{header|AppleScript}}==
[[Category:AppleScript]]
'''Libraries:''' [[Satimage.osax]]
'''Libraries:''' [[Satimage.osax]]


Line 66: Line 65:
}
}


==[[Java]]==
=={{header|Java}}==
[[Category:Java]]
'''Java info:''' java version "1.5.0_06", Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05), Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)
'''Java info:''' java version "1.5.0_06", Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05), Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)


Line 86: Line 84:
// result is now "I am the modified string"
// result is now "I am the modified string"


==[[JavaScript]]==
=={{header|JavaScript}}==
[[Category:JavaScript]]
Test/Match
Test/Match
var subject = "Hello world!";
var subject = "Hello world!";
Line 112: Line 109:
var newSubject = subject.replace(re_PatternToMatch, "Replaced");
var newSubject = subject.replace(re_PatternToMatch, "Replaced");


==[[Perl]]==
=={{header|Perl}}==
[[Category:Perl]]


'''Interpreter:''' [[Perl]] v5.8.8
'''Interpreter:''' [[Perl]] v5.8.8
Line 151: Line 147:
$string =~ s/i/u/ig; # would change "I am a string" into "u am a strung"
$string =~ s/i/u/ig; # would change "I am a string" into "u am a strung"


==[[PHP]]==
=={{header|PHP}}==

[[Category:PHP]]


'''Interpreter:''' [[PHP]] 5.2.0
'''Interpreter:''' [[PHP]] 5.2.0
Line 170: Line 164:
echo "Found 'a' and replace it with 'another', resulting in this string: $string\n";
echo "Found 'a' and replace it with 'another', resulting in this string: $string\n";


==[[Python]]==
=={{header|Python}}==
[[Category:Python]]


'''Interpreter:''' [[Python]] 2.5
'''Interpreter:''' [[Python]] 2.5
Line 202: Line 195:
'''Note:''' re.match() and regex.match() imply a "^" at the beginning of the regular expression. re.search() and regex.search() do not.
'''Note:''' re.match() and regex.match() imply a "^" at the beginning of the regular expression. re.search() and regex.search() do not.


==[[Raven]]==
=={{header|Raven}}==
[[Category:Raven]]


'i am a string' as str
'i am a string' as str
Line 216: Line 208:
str r/ a / another / print
str r/ a / another / print


==[[Ruby]]==
=={{header|Ruby}}==
[[Category:Ruby]]
Test
Test
string="I am a string"
string="I am a string"
Line 236: Line 227:
end)
end)


==[[Tcl]]==
=={{header|Tcl}}==
[[Category:Tcl]]


Test
Test
Line 255: Line 245:
puts [regsub -- { a } {I am a string} { another }]
puts [regsub -- { a } {I am a string} { another }]


==[[Toka]]==
=={{header|Toka}}=
[[Category:Toka]]


Toka's regular expression library allows for matching, but does not yet provide for replacing elements within strings.
Toka's regular expression library allows for matching, but does not yet provide for replacing elements within strings.

Revision as of 04:06, 12 November 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

AppleScript

Libraries: Satimage.osax

try
    find text ".*string$" in "I am a string" with regexp
on error message
    return message
end try
try
    change "original" into "modified" in "I am the original string" with regexp
on error message
    return message
end try

C++

  • Compiler: g++ 4.0.2
  • Libraries: boost
 #include <iostream>
 #include <string>
 #include <iterator>
 #include <boost/regex.hpp>
 
 int main()
 {
   boost::regex re(".* string$");
   std::string s = "Hi, I am a string";
 
   // match the complete string
   if (boost::regex_match(s, re))
     std::cout << "The string matches.\n";
   else
     std::cout << "Oops - not found?\n";
 
   // match a substring
   boost::regex re2(" a.*a");
   boost::smatch match;
   if (boost::regex_search(s, match, re2))
   {
     std::cout << "Matched " << match.length()
               << " characters starting at " << match.position() << ".\n";
     std::cout << "Matched character sequence: \""
               << match.str() << "\"\n";
   }
   else
   {
     std::cout << "Oops - not found?\n";
   }
 
   // replace a substring
   std::string dest_string;
   boost::regex_replace(std::back_inserter(dest_string),
                        s.begin(), s.end(),
                        re2,
                        "'m now a changed");
   std::cout << dest_string << std::endl;
 }

Java

Java info: java version "1.5.0_06", Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05), Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)

Test

String str = "I am a string";
if (str.matches(".*string$")) {
  System.out.println("ends with 'string'");
}

Substitute

String orig = "I am the original string";
Pattern pat = Pattern.compile("original"); // match "original"
Matcher mat = pat.matcher(orig); // get the Matcher

String result = mat.replaceAll("modified"); // replace all matches against the pattern with "modified"
// result is now "I am the modified string"

JavaScript

Test/Match

     var subject = "Hello world!";
     
     // Two different ways to create the RegExp object
     // Both examples use the exact same pattern... matching "hello"
     var re_PatternToMatch = /Hello (World)/i; // creates a RegExp literal with case-insensitivity
     var re_PatternToMatch2 = new RegExp("Hello (World)", "i");
     
     // Test for a match - return a bool
     var isMatch = re_PatternToMatch.test(subject);
     
     // Get the match details
     //    Returns an array with the match's details
     //    matches[0] == "Hello world"
     //    matches[1] == "world"
     var matches = re_PatternToMatch2.exec(subject);

Substitute

     var subject = "Hello world!";
     
     // Perform a string replacement
     //    newSubject == "Replaced!"
     var newSubject = subject.replace(re_PatternToMatch, "Replaced");

Perl

Interpreter: Perl v5.8.8

Test

$string = "I am a string";
if ($string =~ /string$/) {
  print "Ends with 'string'\n";
}

if ($string !~ /^You/) {
  print "Does not start with 'You'\n";
}

Substitute

$string = "I am a string";
$string =~ s/ a / another /; # makes "I am a string" into "I am another string"
print $string;

Test and Substitute

$string = "I am a string";
if ($string =~ s/\bam\b/was/) { # \b is a word border
  print "I was able to find and replace 'am' with 'was'\n";
}

Options

# add the following just after the last / for additional control
# g = globaly (match as many as possible)
# i = case-insensitive
# s = treat all of $string as a single line (incase 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"

PHP

Interpreter: PHP 5.2.0

$string = 'I am a string';

Test

if (preg_match('/string$/', $string))
{
    echo "Ends with 'string'\n";
}

Replace

$string = preg_replace('/\ba\b/', 'another', $string);
echo "Found 'a' and replace it with 'another', resulting in this string: $string\n";

Python

Interpreter: Python 2.5

Setup

import re
str = 'I am a string'

Test

if re.search(r'string$', str):
    print "Ends with 'string'"

Test, storing the compiled regular expression in a variable

regex = re.compile(r'string$')
if regex.search(str):
    print "Ends with 'string'"

Substitute

str = re.sub(r' a ', ' another ', str)

Substitute, storing the compiled regular expression in a variable

regex = re.compile(r' a ')
str = regex.sub(' another ', str)

Note: re.match() and regex.match() imply a "^" at the beginning of the regular expression. re.search() and regex.search() do not.

Raven

'i am a string' as str

Match:

str m/string$/
if  "Ends with 'string'\n" print

Replace:

str r/ a / another / print

Ruby

Test

 string="I am a string"
 puts "Ends with 'string'" if string[/string$/]
 puts "Does not start with 'You'" if !string[/^You/]

Substitute

 puts string.gsub(/ a /,' another ')
 #or
 string[/ a /]='another'
 puts string

Substitute using block

 puts(string.gsub(/\bam\b/) do |match|
        puts "I found #{match}"
        #place "was" instead of the match
        "was"
      end)

Tcl

Test

set theString "I am a string"
if {[regexp -- {string$} $theString]} {
  puts "Ends with 'string'\n"
}

if (![regexp -- {^You} $theString]) {
  puts "Does not start with 'You'\n"
}

Substitute

set theString = "I am a string"
puts [regsub -- { a } {I am a string} { another }]

=Toka

Toka's regular expression library allows for matching, but does not yet provide for replacing elements within strings.

#! Include the regex library
needs regex

#! The two test strings
" This is a string" is-data test.1
" Another string" is-data test.2

#! Create a new regex named 'expression' which tries
#! to match strings beginning with 'This'.
" ^This" regex: expression

#! An array to store the results of the match 
#! (Element 0 = starting offset, Element 1 = ending offset of match)
2 cells is-array match

#! Try both test strings against the expression. 
#! try-regex will return a flag.  -1 is TRUE, 0 is FALSE
expression test.1 2 match try-regex .
expression test.2 2 match try-regex .