Regular Expression Question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Regular Expression Question

pbohuch
Hi everyone,

I am new to smalltalk and have been having trouble using regular expressions. I have installed Vassili Bykov’s RegEx package using the Monticello Browser. Here is what my issue is. I have a test string that I want to "push" into an OrderedCollection.

testString := 'this is a test'.
testCollection := '(^\s*)|(\s)|\w+' asRegex matchesIn: testString.

Now I can inspect this in a workspace and it shows me an ordered collection of all the elements of the string. However when I attempt to use this in a method, and call the method from a TestCase, I get an infinite loop. Stepping through the method I found that the "asRegex" portion of the statement above works correctly. Where is seems to be crashing is in the "matchesIn:" method. Can anyone please let me know what I am missing? The exact statements in my test case are:


| myTokens myString|

self halt.
myTokens := OrderedCollection new.
myString := 'this is a test'.

myTokens :=  '(^\s*)|(\s)|\w+' asRegex matchesIn: myString.
self shouldnt: [myTokens isEmpty].


If you step through this method in the Test Runner you will see it never makes it through the matchesIn: method. Am I forgetting to instantiate something?

Reply | Threaded
Open this post in threaded view
|

Re: Regular Expression Question

Zulq Alam-2
pbohuch wrote:

>
> testString := 'this is a test'.
> testCollection := '(^\s*)|(\s)|\w+' asRegex matchesIn: testString.
>

If I execute this in a workspace, I get an infinite loop. I think it's a
problem with the expression - don't ^\s* and \w+ look for the same
thing? Anyway, wouldn't '\w+|\s+' do what you want?

'this is a test' allRegexMatches: '\w+|\s+'
        "=> ('this' ' ' 'is' ' ' 'a' ' ' 'test')"

Or if you just want the words, '\w+'. And, if you just want the words
then you can probably just use #findTokens:

'this is a test' findTokens: ' '
        "=> ('this' 'is' 'a' 'test')"

Regards,
Zulq.

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners