Lookaround Regex to manipulate Strings

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

Lookaround Regex to manipulate Strings

Hans Gruber
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.

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

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Lookaround Regex to manipulate Strings

cedreek

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.

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?

#sansPeriodSuffix can help

or if you want only some extension:
fileName regex: '.+(=?.(jpg|jpeg|png))' matchesDo: [:fname | newFileName:= fname sansPeriodSuffix]

you probably can do it only with regex but I don't know right now :)

hth,


 

Thanks guys!

Regards,
Hans
______________________________________________________
GRATIS für alle WEB.DE-Nutzer: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://movieflat.web.de

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



--
Cédrick

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Lookaround Regex to manipulate Strings

Andreas.Raab
In reply to this post by Hans Gruber
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

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Re: Lookaround Regex to manipulate Strings

Hans Gruber
In reply to this post by Hans Gruber
Hi Guys!

Thank you so much, this worked fine!

Regards,
Hans

> 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
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>


________________________________________________________________________
Kostenlos tippen, täglich 1 Million gewinnen: zum WEB.DE MillionenKlick!
http://produkte.web.de/go/08/

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Lookaround Regex to manipulate Strings

Andreas.Raab
Hans Gruber wrote:
> Hi Guys!
>
> Thank you so much, this worked fine!

You're welcome. Do yourself a favor and browse around a little in the
various String protocols. There's a *ton* of useful stuff like
allButFirst:, allButLast:, copyUpTo:, copyUpToLast:, copyAfter:,
copyAfterLast: etc. So for example:

   baseName := fileName allButLast: 4. "strips off .foo if you know it's
3 chars but copyUpToLast: is safer here"
   extension := fileName copyAfterLast: $. "gets the .extension"

etc.

Cheers,
   - Andreas


> Regards,
> Hans
>
>> 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
>> _______________________________________________
>> Beginners mailing list
>> [hidden email]
>> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>>
>
>
> ________________________________________________________________________
> Kostenlos tippen, täglich 1 Million gewinnen: zum WEB.DE MillionenKlick!
> http://produkte.web.de/go/08/

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