# Regex problem with gsub

**URL:** <https://www.ruby-forum.com/t/regex-problem-with-gsub/115243>\
**Category:** Ruby\
**Created:** [September 22, 2007, 11:55am UTC](https://www.ruby-forum.com/t/regex-problem-with-gsub/115243 "2007-09-22T11:55:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gilbert\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c68b51/32.png) [@Gilbert\_R](https://www.ruby-forum.com/u/Gilbert_R)\
**Post date:** [September 22, 2007, 11:55am UTC](https://www.ruby-forum.com/t/regex-problem-with-gsub/115243/1 "2007-09-22T11:55:51Z")

</div>

Hi,

i have a dir like that and need the  
first part of the first filename =

J:\test\>dir /B  
20070921\_0001.xml  
20070921\_0002.xml  
20070921\_0003.xml

i tried with =

str=File.basename(Dir[“J:/test/\*.xml”].sort[0]).gsub(/(\w{8}).+/, $1)

but =  
TypeError: can’t convert nil into String  
from (irb):4:in `gsub’  
from (irb):4  
from :0

if i use =  
irb(main):005:0\> str=File.basename(Dir[“J:/test/\*.xml”].sort[0])  
=\> “20070921\_0001.xml”  
i get the filename

how to change my gsub part to make it work, i want  
the first part of the filename, i.e.  
filename “20070921\_0001.xml”  
i want “20070921”

Regards, Gilbert

---

<div class="post-metadata">

**Author:** ![Gilbert\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c68b51/32.png) [@Gilbert\_R](https://www.ruby-forum.com/u/Gilbert_R)\
**Post date:** [September 22, 2007, 12:19pm UTC](https://www.ruby-forum.com/t/regex-problem-with-gsub/115243/2 "2007-09-22T12:19:53Z")

</div>

Gilbert R. wrote:

> str=File.basename(Dir[“J:/test/\*.xml”].sort[0]).gsub(/(\w{8}).+/, $1)

At this point $1 is nil, which is why you get the error. You have to  
keep in  
mind that arguments are evaluated before the method is called and $1  
will  
only have value after that. You need to use ‘\1’ instead of $1.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![Gilbert\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c68b51/32.png) [@Gilbert\_R](https://www.ruby-forum.com/u/Gilbert_R)\
**Post date:** [September 22, 2007, 12:35pm UTC](https://www.ruby-forum.com/t/regex-problem-with-gsub/115243/3 "2007-09-22T12:35:53Z")

</div>

Hi,

Sebastian H. wrote:

> Gilbert R. wrote:
> 
> > str=File.basename(Dir[“J:/test/\*.xml”].sort[0]).gsub(/(\w{8}).+/, $1)
> 
> At this point $1 is nil, which is why you get the error. You have to keep in  
> mind that arguments are evaluated before the method is called and $1 will  
> only have value after that. You need to use ‘\1’ instead of $1.

Thanks!!  
Works now. I’m still not sure, when to use \1 and when to use $1

Regards, Gilbert

---

<div class="post-metadata">

**Author:** ![Gilbert\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c68b51/32.png) [@Gilbert\_R](https://www.ruby-forum.com/u/Gilbert_R)\
**Post date:** [September 22, 2007, 1:15pm UTC](https://www.ruby-forum.com/t/regex-problem-with-gsub/115243/4 "2007-09-22T13:15:57Z")

</div>

Hi Gilbert:

The problem it’s not the regex, it’s the second parameter, try instead:

string.gsub(/(\w{7}).+/, ‘\1’)

This supposing you want ever the first seven chars, if you want the  
characters before because they can be variable, try this regex:

string.gsub(/(\w\*)(\_._)_..+/, ‘\1’)

I’m not mastered the regular expressions, any corrections appreciated.

Regards,

Jovino

-----Mensaje original-----  
De: Gilbert R. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]  
Enviado el: sábado, 22 de septiembre de 2007 11:55  
Para: ruby-talk ML  
Asunto: regex problem with gsub

Hi,

i have a dir like that and need the  
first part of the first filename =

J:\test\>dir /B  
20070921\_0001.xml  
20070921\_0002.xml  
20070921\_0003.xml

i tried with =

str=File.basename(Dir[“J:/test/\*.xml”].sort[0]).gsub(/(\w{8}).+/, $1)

but =  
TypeError: can’t convert nil into String  
from (irb):4:in `gsub’  
from (irb):4  
from :0

if i use =  
irb(main):005:0\> str=File.basename(Dir[“J:/test/\*.xml”].sort[0])  
=\> “20070921\_0001.xml”  
i get the filename

how to change my gsub part to make it work, i want  
the first part of the filename, i.e.  
filename “20070921\_0001.xml”  
i want “20070921”

Regards, Gilbert
