[Newbies] Re: Lookaround Regex to manipulate Strings

Andreas Raab andreas.raab at gmx.de
Thu Jan 14 01:10:54 UTC 2010


Hans Gruber wrote:
> Hi Squeak Fans!
> 
> I have a short question regarding regular expressions in Squeak.
> 
> I have the following problem:
> 
> I have a fileName and I want to cut off its file extension (.jpg, .png etc.). To do this efficiently I wanted to use regular expressions with lookaround.

Using regular expressions may be convenient, it sure as hell isn't 
efficient. Much easier to use, e.g.,

   fileName copyUpToLast: $.

As for efficiency:

[1 to: 10000 do:[:i|
	'filename.jpg' matchesRegex: '.+(=?.(jpg|jpeg|png))'
]] timeToRun.
=> 2222 msecs

[1 to: 10000 do:[:i|
	'filename.jpg' copyUpToLast: $.
]] timeToRun.
=> 30 msecs

So using regexp's is about 80x slower.

Cheers,
   - Andreas


> So let's assume the following:
> 
> | fileName |
> fileName := 'test.jpg'.
> 
> fileName matchesRegex: '.+(=?.(jpg|jpeg|png))'
> 
> I thought there is a possibility of returning the string without file extension through lookaround.
> 
> Do you have any hints for me?
> 
> Thanks guys!
> 
> Regards,
> Hans
> ______________________________________________________
> GRATIS für alle WEB.DE-Nutzer: Die maxdome Movie-FLAT!
> Jetzt freischalten unter http://movieflat.web.de



More information about the Beginners mailing list