Forum: Ruby regular expression / gsub question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cf63da956b6ba955687a2f2f262928cb?d=identicon&s=25 Mmcolli00 Mom (mmcolli00)
on 2009-03-13 20:35
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(/_.*/,"")
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2009-03-13 20:49
(Received via mailing list)
Mmcolli00 Mom wrote:
> 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.
Cf63da956b6ba955687a2f2f262928cb?d=identicon&s=25 Mmcolli00 Mom (mmcolli00)
on 2009-03-13 21:17
Thanks! This worked nicely. I have never used #[], this is great! Thanks
so much!!
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-03-14 11:02
(Received via mailing list)
On 13.03.2009 20:46, Joel VanderWerf wrote:
>> userfilename3_scriptname1_031109.txt
> 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
Bc676b4c4f934105ca524a4547f94f51?d=identicon&s=25 Milan Dobrota (milandobrota)
on 2009-03-14 11:18
You can also do a filename.split(/_/)[1] which is probably not that
efficient but you can get all three parts of the string.
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-03-14 13:24
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?
Bc676b4c4f934105ca524a4547f94f51?d=identicon&s=25 Milan Dobrota (milandobrota)
on 2009-03-14 13:54
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.
This topic is locked and can not be replied to.