Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > regular expression / gsub question

Reply
Thread Tools

regular expression / gsub question

 
 
Mmcolli00 Mom
Guest
Posts: n/a
 
      03-13-2009
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-forum.com/.

 
Reply With Quote
 
 
 
 
Joel VanderWerf
Guest
Posts: n/a
 
      03-13-2009
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

 
Reply With Quote
 
 
 
 
Mmcolli00 Mom
Guest
Posts: n/a
 
      03-13-2009
Thanks! This worked nicely. I have never used #[], this is great! Thanks
so much!!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      03-14-2009
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
 
Reply With Quote
 
Milan Dobrota
Guest
Posts: n/a
 
      03-14-2009
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-forum.com/.

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      03-14-2009
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-forum.com/.

 
Reply With Quote
 
Milan Dobrota
Guest
Posts: n/a
 
      03-14-2009
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-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
Replacing part of a matched regular expression using gsub Ben Ruby 4 03-25-2008 10:27 AM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
gsub and gsub! are inconsistent aurelianito Ruby 9 11-09-2005 01:38 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57