Difference between revisions of "Regular expressions"

From CodeCodex

(added ruby)
Line 1: Line 1:
=Introduction=
+
==Introduction==
  
 
Regular Expressions are a way to search strings. There are implementations in various languages.
 
Regular Expressions are a way to search strings. There are implementations in various languages.
  
=Implementations=
+
==Implementations==
  
==Haskell==
+
===Haskell===
  
<HIGHLIGHTSYNTAX language = "haskell">
+
<pre class="haskell">
 
import Text.Regex
 
import Text.Regex
 
case matchRegex (mkRegex "cat") "cat dog mouse" of
 
case matchRegex (mkRegex "cat") "cat dog mouse" of
 
   Just _  -> True
 
   Just _  -> True
 
   Nothing -> False
 
   Nothing -> False
</HIGHLIGHTSYNTAX>
+
</pre>
  
==Java==
+
===Java===
  
<HIGHLIGHTSYNTAX language = "java122">
+
<pre class="java">
 
boolean found = "cat dog mouse".matches("cat"); // the entire string has to match (not just part of it)
 
boolean found = "cat dog mouse".matches("cat"); // the entire string has to match (not just part of it)
  
Line 22: Line 22:
  
 
boolean found = Pattern.compile("cat").matcher("cat dog mouse").find(); // matches anywhere in the string
 
boolean found = Pattern.compile("cat").matcher("cat dog mouse").find(); // matches anywhere in the string
</HIGHLIGHTSYNTAX>
+
</pre>
  
==JavaScript==
+
===JavaScript===
  
<HIGHLIGHTSYNTAX language = "javascript">
+
<pre class="javascript">
 
var pattern = /cat/;
 
var pattern = /cat/;
  
 
var found = pattern.test("cat dog mouse");
 
var found = pattern.test("cat dog mouse");
</HIGHLIGHTSYNTAX>
+
</pre>
  
==OCaml==
+
===OCaml===
  
 
You need to first link with the Str library
 
You need to first link with the Str library
<HIGHLIGHTSYNTAX language = "ocaml">
+
<pre class="ocaml">
 
#load "str.cma";; (* run this when in interactive mode *)
 
#load "str.cma";; (* run this when in interactive mode *)
 
try
 
try
Line 41: Line 41:
 
   true
 
   true
 
with Not_found -> false
 
with Not_found -> false
</HIGHLIGHTSYNTAX>
+
</pre>
  
==Perl==
+
===Perl===
  
<HIGHLIGHTSYNTAX language = "perl">
+
<pre class="perl">
 
$found = "cat dog mouse" =~ /cat/;
 
$found = "cat dog mouse" =~ /cat/;
</HIGHLIGHTSYNTAX>
+
</pre>
  
==PHP==
+
===PHP===
  
<HIGHLIGHTSYNTAX language = "php3">
+
<pre class="php">
 
$found = preg_match('/cat/', "cat dog mouse");
 
$found = preg_match('/cat/', "cat dog mouse");
</HIGHLIGHTSYNTAX>
+
</pre>
  
==Python==
+
===Python===
  
 
Here is howto get a boolean result for a search.
 
Here is howto get a boolean result for a search.
<HIGHLIGHTSYNTAX language = "python">
+
<pre class="python">
 
import re
 
import re
  
 
found = re.search('cat', 'cat dog mouse')
 
found = re.search('cat', 'cat dog mouse')
</HIGHLIGHTSYNTAX>
+
</pre>
  
==Ruby==
+
===Ruby===
  
 
Here is howto get a boolean result for a search.
 
Here is howto get a boolean result for a search.
<HIGHLIGHTSYNTAX language = "ruby">
+
<pre class="ruby">
found = ('cat dog mouse' =~ /cat/) != nil
+
found = !!(/cat/ =~ 'cat dog mouse')
</HIGHLIGHTSYNTAX>
+
</pre>
  
=Useful Links=  
+
==Useful Links==
  
 
http://www.regular-expressions.info/
 
http://www.regular-expressions.info/

Revision as of 05:51, 23 January 2011

Introduction

Regular Expressions are a way to search strings. There are implementations in various languages.

Implementations

Haskell

import Text.Regex
case matchRegex (mkRegex "cat") "cat dog mouse" of
  Just _  -> True
  Nothing -> False

Java

boolean found = "cat dog mouse".matches("cat"); // the entire string has to match (not just part of it)

import java.util.regex.*;

boolean found = Pattern.compile("cat").matcher("cat dog mouse").find(); // matches anywhere in the string

JavaScript

var pattern = /cat/;

var found = pattern.test("cat dog mouse");

OCaml

You need to first link with the Str library

#load "str.cma";; (* run this when in interactive mode *)
try
  ignore(Str.search_forward (Str.regexp "cat") "cat dog mouse" 0);
  true
with Not_found -> false

Perl

$found = "cat dog mouse" =~ /cat/;

PHP

$found = preg_match('/cat/', "cat dog mouse");

Python

Here is howto get a boolean result for a search.

import re

found = re.search('cat', 'cat dog mouse')

Ruby

Here is howto get a boolean result for a search.

found = !!(/cat/ =~ 'cat dog mouse')

Useful Links

http://www.regular-expressions.info/