background image
C
ATS
(
TM
) P
ROGRAMMING
 L
ANGUAGE
 O
VERVIEW
As revised 2006.08.23
Bionic Buffalo Tech Note #84 
Security: 
Unrestricted
regex
 is the type of a compiled compiled regular expression. The implementation of a 
regex
 is 
opaque. A 
string
 may be cast to a 
regex
, and vice versa. 
3.3.  Regular Expressions and String Operations 
In addition to understanding the + operator for concatenation, Cats also understands some other string 
operations.   
3.3.1.  Pattern Matching 
The expression  
string1 $* regex1 $: options1
 
where all three arguments are strings, returns a 
list
 of matched strings. This is similar to the Perl 
expression 
$string1 =~ m/($regex1)/<options1>
. The strings within the 
list
 are 
equivalent to 
$1
$2
, and so on, in Perl. If there are no matches, or if there are no capturing parentheses, 
then an empty 
list
 is returned. After the operation, the predefined variable 
count
 contains the 
number of matches. For example:
string a = “wildebeest feast” ; 
list (string) b ; 
b = a $* “(e[ae]st)” $: “g” ; 
// b contains “eest” and “east” 
The match/assignment operator 
$*=
 is defined, in the same was as are 
+=
­=
, and so on. 
3.3.2.  Substitution 
The expression 
string1 $% pattern1 $/ replacement1 $: options 
 
returns the value of 
string1
 after the substitution. After execution, the predefined variable 
count 
contains the number of substitutions. 
The Perl expression 
$string1 =~ s/$pattern1/$replacement1/<options1>
 alters the 
value of 
$string1
. By contrast, the equivalent Cats expression returns the altered value, without 
affecting the original value of the 
string1
 argument. A Cats program can use the 
substitute/assignment operator 
$%=
 (in the same way that 
+=
 and 
­=
 are used) to change the value of 
the 
string1
 argument.   
Copyright 2006 Bionic Buffalo. All rights reserved.
File tn0084; Modified 2006­08­30 08:00:17
http://www.tatanka.com/doc/technote/index.html
E­mail: 
query@tatanka.com
Page 6 of 12