help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-smalltalk] [rfc] regex rewrite


From: Mike Anderson
Subject: Re: [Help-smalltalk] [rfc] regex rewrite
Date: Mon, 30 May 2005 23:45:20 +0000
User-agent: Mozilla Thunderbird 0.7.3 (X11/20040803)

Paolo Bonzini wrote:

I also think it would be more useful if =~ returned MatchResult/nil instead of true/false.

There's #searchRegex: for that. The "=~" name seemed more like a boolean query to me.

There's #matchRegex: '.*whatever.*' for that :)

I just don't think that what it does is sufficiently useful to devote the =~ operator to it.

Both Perl and Ruby return values that can be interpreted as true/false from =~, but both of them also capture the matched expression and sub-expressions into variables. We don't have that possibility, so I think =~ should work harder.

Here is a new version of the patch (only the regex.st parts) that supports cute things like

st> ^'abc' copyFrom: 1 to: 3 replacingRegex: 'a(.)c' with: 'X%1Z'!
'XbZ'

That is nice. I did wonder whether backslashes would be better, but eventually decided that I could live with percents.

BTW there is a missing period in #copyFrom:to:replacingAllRegex:with:.

Mike
--- smalltalk-2.1g-regex/examples/regex.st      2005-05-30 17:05:02.399926760 
+0000
+++ smalltalk-2.1g-mod/examples/regex.st        2005-05-30 23:33:38.161392248 
+0000
@@ -436,19 +436,21 @@
     regex := pattern asRegex.
     res := WriteStream on: (String new: to - from + 1).
     idx := from.
-    [
-        regs := self searchRegex: regex from: idx to: to.
-        beg >= 1
-       regs notNil
-    ] whileTrue: [
-       beg := regs from.
-       end := regs to.
-       res next: beg - idx putAll: self startingAt: idx.
-       res nextPutAll: (str bindWithArguments: regs).
-       idx := end + 1.
-       beg > end ifTrue: [ res nextPut: (self at: idx). idx := idx + 1 ].
-       idx > self size ifTrue: [ ^res contents ].
-    ].
+    [  regs := self searchRegex: regex from: idx to: to.
+        beg >= 1.
+               regs notNil     ] 
+               whileTrue: 
+               [ beg := regs from.
+               end := regs to.
+               res next: beg - idx putAll: self startingAt: idx.
+               res nextPutAll: (str bindWithArguments: regs).
+               idx := end + 1.
+               beg > end 
+                       ifTrue: 
+                               [ res nextPut: (self at: idx). 
+                               idx := idx + 1 ].
+               idx > self size 
+                       ifTrue: [ ^res contents ]. ].
     res next: to - idx + 1 putAll: self startingAt: idx.
 
     ^res contents

reply via email to

[Prev in Thread] Current Thread [Next in Thread]