# Regular expression / gsub question

**URL:** <https://www.ruby-forum.com/t/regular-expression-gsub-question/162794>\
**Category:** Ruby\
**Created:** [March 13, 2009, 8:35pm UTC](https://www.ruby-forum.com/t/regular-expression-gsub-question/162794 "2009-03-13T20:35:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [March 13, 2009, 8:35pm UTC](https://www.ruby-forum.com/t/regular-expression-gsub-question/162794/1 "2009-03-13T20:35:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [March 13, 2009, 8:49pm UTC](https://www.ruby-forum.com/t/regular-expression-gsub-question/162794/2 "2009-03-13T20:49:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [March 13, 2009, 9:17pm UTC](https://www.ruby-forum.com/t/regular-expression-gsub-question/162794/3 "2009-03-13T21:17:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [March 14, 2009, 11:18am UTC](https://www.ruby-forum.com/t/regular-expression-gsub-question/162794/4 "2009-03-14T11:18:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [March 14, 2009, 11:02am UTC](https://www.ruby-forum.com/t/regular-expression-gsub-question/162794/5 "2009-03-14T11:02:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [March 14, 2009, 1:24pm UTC](https://www.ruby-forum.com/t/regular-expression-gsub-question/162794/6 "2009-03-14T13:24:04Z")

</div>

Milan D. 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?

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [March 14, 2009, 1:54pm UTC](https://www.ruby-forum.com/t/regular-expression-gsub-question/162794/7 "2009-03-14T13:54:26Z")

</div>

7stud – wrote:

> Milan D. 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.
