[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

regular expression / gsub question

Mmcolli00 Mom

3/13/2009 7:34:00 PM

Hi
Do you know how use a regular expression to get only the scripname from
the each filename below? I have long filename and I want to pull out a
segment "scriptname" only. I have been using a regular expression with
gsub for this.

filename

userfilename_scriptname_030109.txt
userfilename3_scriptname1_031109.txt
userfilename_scriptname0_031209.txt


The gsub didn't work because the _ on both sides causes me to delete the
whole filename. What would you recommend?

stripfirstpart = filename.gsub(/*_/,"")
stripsecondpart = filename.gsub(/_.*/,"")
--
Posted via http://www.ruby-....

6 Answers

Joel VanderWerf

3/13/2009 7:46:00 PM

0

Mmcolli00 Mom wrote:
> Hi
> Do you know how use a regular expression to get only the scripname from
> the each filename below? I have long filename and I want to pull out a
> segment "scriptname" only. I have been using a regular expression with
> gsub for this.
>
> filename
>
> userfilename_scriptname_030109.txt
> userfilename3_scriptname1_031109.txt
> userfilename_scriptname0_031209.txt
>
>
> The gsub didn't work because the _ on both sides causes me to delete the
> whole filename. What would you recommend?
>
> stripfirstpart = filename.gsub(/*_/,"")
> stripsecondpart = filename.gsub(/_.*/,"")

I like using #[] for this because it lets you think in terms of what you
want to keep rather than what you want to remove.

filename[/_(.*?)_/, 1]

The ? is there in case there are more underscores later in the filename.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Mmcolli00 Mom

3/13/2009 8:15:00 PM

0

Thanks! This worked nicely. I have never used #[], this is great! Thanks
so much!!
--
Posted via http://www.ruby-....

Robert Klemme

3/14/2009 9:59:00 AM

0

On 13.03.2009 20:46, Joel VanderWerf wrote:
> Mmcolli00 Mom wrote:
>> Hi
>> Do you know how use a regular expression to get only the scripname from
>> the each filename below? I have long filename and I want to pull out a
>> segment "scriptname" only. I have been using a regular expression with
>> gsub for this.
>>
>> filename
>>
>> userfilename_scriptname_030109.txt
>> userfilename3_scriptname1_031109.txt
>> userfilename_scriptname0_031209.txt
>>
>>
>> The gsub didn't work because the _ on both sides causes me to delete the
>> whole filename. What would you recommend?
>>
>> stripfirstpart = filename.gsub(/*_/,"")
>> stripsecondpart = filename.gsub(/_.*/,"")
>
> I like using #[] for this because it lets you think in terms of what you
> want to keep rather than what you want to remove.
>
> filename[/_(.*?)_/, 1]
>
> The ? is there in case there are more underscores later in the filename.

AFAIK it is more robust and also more efficient to do

filename[/_([^_]+)_/, 1]

or even

filename[/_(scriptname\d*)_/, 1]

or even

filename[/\Auserfilename\d*_(scriptname\d*)_\d+\.txt\z/, 1]

In other words, rather explicitly define precisely what you want to
match than rely on (non)greediness of repetition operators.

Kind regards

robert

Milan Dobrota

3/14/2009 10:17:00 AM

0

You can also do a filename.split(/_/)[1] which is probably not that
efficient but you can get all three parts of the string.
--
Posted via http://www.ruby-....

7stud --

3/14/2009 12:22:00 PM

0

Milan Dobrota wrote:
> You can also do a filename.split(/_/)[1]
>
> which is probably not that
> efficient but you can get all three parts of the string.

Do you mean "not that efficient" in the sense that ruby programs are
ponderously slow and that isn't?
--
Posted via http://www.ruby-....

Milan Dobrota

3/14/2009 12:52:00 PM

0

7stud -- wrote:
> Milan Dobrota wrote:
>> You can also do a filename.split(/_/)[1]
>>
>> which is probably not that
>> efficient but you can get all three parts of the string.
>
> Do you mean "not that efficient" in the sense that ruby programs are
> ponderously slow and that isn't?
I still believe that
filename[/\Auserfilename\d*_(scriptname\d*)_\d+\.txt\z/, 1]
is the most efficient way of doing that. I just provide this as another
option.
--
Posted via http://www.ruby-....