regular expression / gsub question

M

Mmcolli00 Mom

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(/_.*/,"")
 
J

Joel VanderWerf

Mmcolli00 said:
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.
 
M

Mmcolli00 Mom

Thanks! This worked nicely. I have never used #[], this is great! Thanks
so much!!
 
R

Robert Klemme

Mmcolli00 said:
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
 
M

Milan Dobrota

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

7stud --

Milan said:
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?
 
M

Milan Dobrota

7stud said:
Milan said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top