# Gsub question on special charaters

**URL:** <https://www.ruby-forum.com/t/gsub-question-on-special-charaters/120806>\
**Category:** Ruby\
**Created:** [November 20, 2007, 7:20pm UTC](https://www.ruby-forum.com/t/gsub-question-on-special-charaters/120806 "2007-11-20T19:20:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [November 20, 2007, 7:20pm UTC](https://www.ruby-forum.com/t/gsub-question-on-special-charaters/120806/1 "2007-11-20T19:20:03Z")

</div>

I have a string that looks like this :

ts = “^LThe beginning of the sentence”

I want to remove the “^L”

I’ve tried

ts.gsub!(/^[^L]/,’’)

which removes the “^”, but I need to remove the “^L” when they are  
together and at the beginning of the sentence.

Any help is welcome

---

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [November 20, 2007, 7:26pm UTC](https://www.ruby-forum.com/t/gsub-question-on-special-charaters/120806/2 "2007-11-20T19:26:29Z")

</div>

On Nov 20, 11:18 am, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> which removes the “^”, but I need to remove the “^L” when they are  
> together and at the beginning of the sentence.

irb(main):001:0\> ts = “^LThe beginning of the sentence”  
=\> “^LThe beginning of the sentence”  
irb(main):002:0\> ts.sub /^^L/, ‘’  
=\> “The beginning of the sentence”

Also:

1. Do you really mean ^ (start of line) or do you mean \A (start of  
string)?
2. No need to use gsub if there’s only ever going to be a single  
match.

---

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [November 20, 2007, 7:32pm UTC](https://www.ruby-forum.com/t/gsub-question-on-special-charaters/120806/3 "2007-11-20T19:32:22Z")

</div>

Yours doesn’t work because characters in the square brackets can’t be  
escaped. Instead of matching that character, it will match “”, “^”,  
or “L”. Phrogz has the right answer; just thought I’d explain why his  
works and yours doesn’t.

Ben

---

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [November 20, 2007, 7:31pm UTC](https://www.ruby-forum.com/t/gsub-question-on-special-charaters/120806/4 "2007-11-20T19:31:10Z")

</div>

ts.gsub!(/^^L/,’’)

---

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [November 20, 2007, 9:30pm UTC](https://www.ruby-forum.com/t/gsub-question-on-special-charaters/120806/5 "2007-11-20T21:30:58Z")

</div>

Thanks! The explanation helped. I just ordered  
Mastering Regular Expressions  
[http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124/ref=pd\_bbs\_sr\_1?ie=UTF8&s=books&qid=1195585371&sr=1-1](http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1195585371&sr=1-1)

Mastering Regular Expressions  
[http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124/ref=pd\_bbs\_sr\_1?ie=UTF8&s=books&qid=1195585371&sr=1-1](http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1195585371&sr=1-1)  
by Jeffrey Friedl

## – Robert K.

removed\_email\_address@domain.invalid
