GST Regex

Smalltalk by example: Regular expressions

GNU Smalltalk 2.3 will have Regex in the image by default, when that is released this needs a rewrite

Introduction

There seem to be multiple regular expression matchers in the examples directory or GNU Smalltalk 2.2. RegExp.st and regex.st. The second links to the Perl regular expressions function, the first seems to be a pure smalltalk implementation. I found RegExp.st to be incomplete. Although a pure Smalltalk implementation of regular expressions might some day come, regex is the best at the moment.

The regex package will introduce the Smalltalk.Regex class definition. To load the package, get write access to the Smalltalk image and then issue gst -qK Load.st -a Regex. After this, you should be able to succesfully run Regex inspect ! within gst.

Initialization

fromString is probably the best initialization to use.

reg := Regex fromString: 'he.lo'. reg asString "Returns the string 'hello'". match := 'hello' =~ reg. "Returns a MatchingRegexResults " match inspect. !
 * reg match|

Matching

For matchin a Regex to a pattern, the special ~= binary operator has been created.

Back to GST by example