# Simple regex question

**URL:** <https://www.ruby-forum.com/t/simple-regex-question/171108>\
**Category:** Ruby\
**Created:** [June 26, 2009, 2:52pm UTC](https://www.ruby-forum.com/t/simple-regex-question/171108 "2009-06-26T14:52:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [June 26, 2009, 2:52pm UTC](https://www.ruby-forum.com/t/simple-regex-question/171108/1 "2009-06-26T14:52:08Z")

</div>

Hello.  
I need to parse through thousands of TIFF files and do some re-naming.  
These files have underscores in them followed by a sequential number. I  
need to grab just the “root” of the filename, without the underscore or  
the numbers.  
Dir.chdir(“L:/infocontiffs/ehs-g7917741”)  
files = Dir.glob("_.tiff")  
file = files[0]  
puts file  
file = file.gsub(/^(._)\_[0-9]+.tiff/, “#{$1}”)  
puts file  
What I get with this is:  
ehs-g7917741\_01.tiff  
Why doesn’t it give me my root filename?  
Thanks,  
Peter

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [June 26, 2009, 3:24pm UTC](https://www.ruby-forum.com/t/simple-regex-question/171108/2 "2009-06-26T15:24:25Z")

</div>

Peter B. wrote:

> Hello.  
> I need to parse through thousands of TIFF files and do some re-naming.  
> These files have underscores in them followed by a sequential number. I  
> need to grab just the “root” of the filename, without the underscore or  
> the numbers.  
> Dir.chdir(“L:/infocontiffs/ehs-g7917741”)  
> files = Dir.glob("_.tiff")  
> file = files[0]  
> puts file  
> file = file.gsub(/^(._)\_[0-9]+.tiff/, “#{$1}”)  
> puts file  
> What I get with this is:  
> ehs-g7917741\_01.tiff  
> Why doesn’t it give me my root filename?  
> Thanks,  
> Peter

Is this what you want?

while fname = DATA.gets  
m = fname.match /(.\*?)\_\d+.tiff/  
if m  
puts “Match: ‘#{m[1]}’”  
else  
puts “No match: #{fname}”  
end  
end

**END**  
ehs-g7917741\_01.tiff  
asadsasd\_12345.tiff  
ljhkjhkh\_1\_2\_3.tiff  
xxxx\_\_1.tiff  
xxxx\_.tiff  
xxxx.tiff  
xxxx  
\_.tiff  
\_01.tiff

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [June 26, 2009, 3:30pm UTC](https://www.ruby-forum.com/t/simple-regex-question/171108/3 "2009-06-26T15:30:58Z")

</div>

Peter B. wrote:

> Hello.  
> I need to parse through thousands of TIFF files and do some re-naming.  
> These files have underscores in them followed by a sequential number. I  
> need to grab just the “root” of the filename, without the underscore or  
> the numbers.  
> Dir.chdir(“L:/infocontiffs/ehs-g7917741”)  
> files = Dir.glob("_.tiff")  
> file = files[0]  
> puts file  
> file = file.gsub(/^(._)\_[0-9]+.tiff/, “#{$1}”)

The argument “#{$1}” is expanded once, before gsub even executes. You  
probably want the block form:

file = file.sub(/^(.\*)\_\d+.tiff/) { $1 }

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [June 26, 2009, 3:50pm UTC](https://www.ruby-forum.com/t/simple-regex-question/171108/4 "2009-06-26T15:50:19Z")

</div>

Hi –

On Fri, 26 Jun 2009, Peter B. wrote:

> puts file  
> What I get with this is:  
> ehs-g7917741\_01.tiff  
> Why doesn’t it give me my root filename?

Here’s another good use of the string[//] technique:

> > file = “ehs-g7917741\_01.tiff”  
> > =\> “ehs-g7917741\_01.tiff”
> 
> > file[/[^\_]+/] # match non-underscore characters  
> > =\> “ehs-g7917741”

David

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [June 26, 2009, 3:52pm UTC](https://www.ruby-forum.com/t/simple-regex-question/171108/5 "2009-06-26T15:52:53Z")

</div>

Beautiful. Thanks.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [June 26, 2009, 4:01pm UTC](https://www.ruby-forum.com/t/simple-regex-question/171108/6 "2009-06-26T16:01:27Z")

</div>

2009/6/26 David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> On Fri, 26 Jun 2009, Peter B. wrote:

> Here’s another good use of the string[//] technique:
> 
> > > file = “ehs-g7917741\_01.tiff”
> 
> =\> “ehs-g7917741\_01.tiff”
> 
> > > file[/[^\_]+/] # match non-underscore characters
> 
> =\> “ehs-g7917741”

Combining all the good suggestions this is probably what I’d do:

files = Dir.glob(“L:/infocontiffs/ehs-g7917741/\*.tiff”)  
files.each do |f|  
base = File.basename f  
root = base[/^([^_]+)_\d+.tiff$/, 1]

if base

# rename or whatever

else  
$stderr.puts “Dunno what to do with #{f}”  
end  
end

The reason I left in the matching of underscores and digits is to be  
sure that the complete name matches the pattern that we required in  
order to detect other files that might accidentally have been placed  
in that directory.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [June 26, 2009, 3:34pm UTC](https://www.ruby-forum.com/t/simple-regex-question/171108/7 "2009-06-26T15:34:59Z")

</div>

Tim H. wrote:

> Peter B. wrote:
> 
> > Hello.  
> > I need to parse through thousands of TIFF files and do some re-naming.  
> > These files have underscores in them followed by a sequential number. I  
> > need to grab just the “root” of the filename, without the underscore or  
> > the numbers.  
> > Dir.chdir(“L:/infocontiffs/ehs-g7917741”)  
> > files = Dir.glob("_.tiff")  
> > file = files[0]  
> > puts file  
> > file = file.gsub(/^(._)\_[0-9]+.tiff/, “#{$1}”)  
> > puts file  
> > What I get with this is:  
> > ehs-g7917741\_01.tiff  
> > Why doesn’t it give me my root filename?  
> > Thanks,  
> > Peter
> 
> Is this what you want?
> 
> while fname = DATA.gets  
> m = fname.match /(.\*?)\_\d+.tiff/  
> if m  
> puts “Match: ‘#{m[1]}’”  
> else  
> puts “No match: #{fname}”  
> end  
> end
> 
> **END**  
> ehs-g7917741\_01.tiff  
> asadsasd\_12345.tiff  
> ljhkjhkh\_1\_2\_3.tiff  
> xxxx\_\_1.tiff  
> xxxx\_.tiff  
> xxxx.tiff  
> xxxx  
> \_.tiff  
> \_01.tiff

Well, you gave me a good idea, using match. Here’s what I did, and, it  
worked. Thank you very much, Tim.

Dir.chdir(“L:/infocontiffs/ehs-g7917741”)  
files = Dir.glob("_.tiff")  
file = files[0]  
puts file  
file = file.match(/^(._)\_[0-9]+.tiff/)  
#file = file.to\_i  
puts $1  
#end  
gives me:  
ehs-g7917741\_01.tiff  
ehs-g7917741

```
    Program exited with code 0
```
