# Gsub help

**URL:** <https://www.ruby-forum.com/t/gsub-help/189893>\
**Category:** Rails\
**Created:** [June 9, 2010, 3:39pm UTC](https://www.ruby-forum.com/t/gsub-help/189893 "2010-06-09T15:39:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon\_comp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@anon\_comp](https://www.ruby-forum.com/u/anon_comp)\
**Post date:** [June 9, 2010, 3:39pm UTC](https://www.ruby-forum.com/t/gsub-help/189893/1 "2010-06-09T15:39:15Z")

</div>

Here’s my code

def index  
file = “BLAH-ex201588.doc”  
@file\_cut = order.gsub(/[\w [- ! # $ % ^ & \* = +]+]+.doc$/,  
'\*.doc)  
end

The code worked before when I was using Ruby 1.9.1, but now I’m using  
Ruby 1.8.7 due to technical reasons. I would imagine that it should  
still work, but I doesn’t. If I removed the nested brackets and it’s  
contents, it’ll work, but will only cut the string to “BLAH-_.doc"  
instead of "_.doc”

Please help.

Thanks,  
Anon\_comp

---

<div class="post-metadata">

**Author:** ![anon\_comp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@anon\_comp](https://www.ruby-forum.com/u/anon_comp)\
**Post date:** [June 9, 2010, 4:40pm UTC](https://www.ruby-forum.com/t/gsub-help/189893/2 "2010-06-09T16:40:44Z")

</div>

I think you should ask this in a Ruby forum. My one cent.

On 9 June 2010 14:37, anon\_comp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> still work, but I doesn’t. If I removed the nested brackets and it’s  
> “Ruby on Rails: Talk” group.  
> To post to this group, send email to removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to  
> removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> .  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

## –

Nandri(Thanks in Tamil),  
Amala S.

---

<div class="post-metadata">

**Author:** ![anon\_comp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@anon\_comp](https://www.ruby-forum.com/u/anon_comp)\
**Post date:** [June 9, 2010, 4:43pm UTC](https://www.ruby-forum.com/t/gsub-help/189893/3 "2010-06-09T16:43:39Z")

</div>

On Jun 9, 9:37 am, anon\_comp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> still work, but I doesn’t. If I removed the nested brackets and it’s  
> contents, it’ll work, but will only cut the string to “BLAH-_.doc"  
> instead of "_.doc”
> 
> Please help.
> 
> Thanks,  
> Anon\_comp

This is absolutely ridiculous lol. This is the second time I’ve asked  
a question and was able to solve it myself. Guess if I just keep  
working for another day, I’ll eventually get the answer -\_\_\_\_-

Thank you all for reading. Don’t need help anymore. All I had to do  
was remove brakets and escape the speical characters dispite the fact  
that documents say that you don’t have to escape them.

Anon\_comp

---

<div class="post-metadata">

**Author:** ![anon\_comp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@anon\_comp](https://www.ruby-forum.com/u/anon_comp)\
**Post date:** [June 9, 2010, 5:18pm UTC](https://www.ruby-forum.com/t/gsub-help/189893/4 "2010-06-09T17:18:47Z")

</div>

On Wed, Jun 9, 2010 at 9:42 AM, anon\_comp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Ruby 1.8.7 due to technical reasons. I would imagine that it should  
> > This is absolutely ridiculous lol. This is the second time I’ve asked  
> > a question and was able to solve it myself. Guess if I just keep  
> > working for another day, I’ll eventually get the answer -\_\_\_\_-

How are we supposed to help you when you haven’t told us what this is  
supposed to do? We could look at it and try to infer patterns and rules  
from  
a single example that is not even real data, but we would have to make  
lots  
of assumptions that could easily turn out to be false, especially since  
you’re saying you expected the result to be ‘\*.doc’ in the summary at  
the  
end. What do you need a regex for at all?

Also you have a syntax error, but it’s easy enough to guess where the  
string  
should have been terminated.

You should answer these questions, because, looking at your regex, I  
doubt  
it does what you intend it to. And if it does do what you intend it to,  
it  
is probably not for the reasons you think.

---

<div class="post-metadata">

**Author:** ![anon\_comp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@anon\_comp](https://www.ruby-forum.com/u/anon_comp)\
**Post date:** [June 9, 2010, 4:46pm UTC](https://www.ruby-forum.com/t/gsub-help/189893/5 "2010-06-09T16:46:35Z")

</div>

On Jun 9, 2:37 pm, anon\_comp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> still work, but I doesn’t. If I removed the nested brackets and it’s  
> contents, it’ll work, but will only cut the string to “BLAH-_.doc"  
> instead of "_.doc”

ruby 1.9 uses a different regular expression. I think that if you want  
to include a - in a character class then it should be the last thing  
in it. /[\w ! # $ % ^ & \* = ±]+.doc$ works for me

Fred

---

<div class="post-metadata">

**Author:** ![anon\_comp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@anon\_comp](https://www.ruby-forum.com/u/anon_comp)\
**Post date:** [June 9, 2010, 5:50pm UTC](https://www.ruby-forum.com/t/gsub-help/189893/6 "2010-06-09T17:50:59Z")

</div>

On Jun 9, 10:45 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ruby 1.9 uses a different regular expression. I think that if you want  
> to include a - in a character class then it should be the last thing  
> in it. /[\w ! # $ % ^ & \* = ±]+.doc$ works for me

Thanks Fred. This is a lot cleaner than the one I have now.

On Jun 9, 11:17 am, Josh C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How are we supposed to help you when you haven’t told us what this is  
> supposed to do? We could look at it and try to infer patterns and rules from  
> a single example that is not even real data, but we would have to make lots  
> of assumptions that could easily turn out to be false, especially since  
> you’re saying you expected the result to be ‘\*.doc’ in the summary at the  
> end. What do you need a regex for at all?

Sorry for not being clear, but I thought it was understandable in the  
end.

> Also you have a syntax error, but it’s easy enough to guess where the string  
> should have been terminated.

Syntax error? You mean my typo at the end of the code? '\*doc) when it  
should be ‘\*doc’) Sorry about that

> You should answer these questions, because, looking at your regex, I doubt  
> it does what you intend it to. And if it does do what you intend it to, it  
> is probably not for the reasons you think.

I think it does do what I indend for it to do. At least it seems so to  
me.

Thanks for replying,  
Anon\_comp
