

Task

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


@1Introduction.
Web 68 has access to a regular expression module 
which can compile regular expressions,
use them for matching strings, 
and replace strings with the matched string.
 
@a@<Compiler prelude@>
BEGIN
@<Declarations@>
@<Logic at the top level@>
END
@<Compiler postlude@>
 
@ The local compiler requires a special prelude.
 
@<Compiler prel...@>=
PROGRAM rosettacode regex CONTEXT VOID
USE regex,standard
 
@ And a special postlude.
 
@<Compiler post...@>=
FINISH
 
@1Regular expressions.
Compile a regular expression and match a string using it.
 
@<Decl...@>=
STRING regexp="string$";
REF REGEX rx=rx compile(regexp);
 
@ Declare a string for the regular expression to match.
 
@<Decl...@>=
STRING to match = "This is a string";
 
@ Define a routine to print the result of matching.
 
@<Decl...@>=
OP MATCH = (REF REGEX rx,STRING match)STRING:
IF rx match(rx,match,LOC SUBEXP)
THEN "matches"
ELSE "doesn't match"
FI;
 
@ Check whether the regular expression matches the string.
 
@<Logic...@>=
print(("String """,regexp,""" ",rx MATCH to match,
       " string """,to match,"""",newline))
 
@ The end.
This program is processed by tang to produce Algol 68 code which has to be compiled by the a68toc compiler.
It's output is then compiled by gcc to produce a binary program. The script 'ca' provided with the Debian
package algol68toc requires the following command to process this program.
  ca -l mod rosettacoderegex.w68
That's it. The resulting binary will print
'String "string$" matches string "This is a string"'