using RegularExpressions for matching (was Re: deficience in Squeak)

Ned Konz ned at squeakland.org
Sat Nov 22 19:24:50 UTC 2003


On Friday 21 November 2003 4:49 pm, ye juan wrote:
> in Squeak, however, I find many basic methods
> disappearing, such=
>  as "matchesPattern: ignoreCase: do:". So I want to
> know whether=
>  this is deficient in Squeak, or there exist some
> better methods=
>  to replace the old ones.

That method's comment says:

	"Answer whether the receiver matches the pattern.  Matching
	includes upper/lower case differences if ignoreCase is false.  Where the
	pattern contains #, the receiver may contain any single character.  Where
	the pattern contains *, the receiver may contain any sequence of characters.
	If aBlock is not nil then before the receiver answers true for the matching case,
	 the block will be evaluated with the start and stop index for the runs matching
	a # or a *.  Note that the block may be evaluated even if the ultimate answer
	is false."

In Squeak (as shipped) we don't have this same method.

Searching for "match" in the Method Finder or Message Names tools
eventually would show you the methods

	String>>match:
and
	String>>startingAt:match:startingAt:

Both of these ignore case.

If you're not providing a block to the VW method, then you can use these.

That is, if your code says:

	(someString matchesPattern: "abc*def" ignoreCase: true do: nil) ifTrue: [ ... ]

you could use this:

	(someString match: "abc*def") ifTrue: [ ... ]

However, if you want case-sensitive matching, or if you're providing a block,
then you have to do something different.

If you load the "Regular Expression Plugin" package from SqueakMap, then you will
be able to use full Perl regular expressions.

If you still wanted to use your VW method, you could define it using the Regular Expression
package as something like:

String>>
matchesPattern: pattern ignoreCase: ignoreCase do: aBlockOrNil
	"Answer whether the receiver matches the pattern.  Matching
	includes upper/lower case differences if ignoreCase is false.  Where the
	pattern contains #, the receiver may contain any single character.  Where
	the pattern contains *, the receiver may contain any sequence of characters.
	If aBlock is not nil then before the receiver answers true for the matching case,
	 the block will be evaluated with the start and stop index for the runs matching
	a # or a *.  Note that the block will not be evaluated if the ultimate answer
	is false (this is a change from the VW semantics)."

	| rePattern re match |
	"Change simple patterns into grouped regex patterns"
	rePattern _ pattern copyReplaceAll: '#' with: '(.)'.
	rePattern _ rePattern copyReplaceAll: '*' with: '(.*)'.
	re _ rePattern asRe.
	re isCaseSensitive: ignoreCase not.
	match _ re search: self.
	match ifNil: [ ^false ].
	aBlockOrNil ifNotNil: [ 1 to: match numGroups do: [ :i |
		aBlockOrNil value: (match fromAt: i) value: (match toAt: i) ]].
	^true





More information about the Squeak-dev mailing list