[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

gsub question on special charaters

rlkeller

11/20/2007 6:19:00 PM


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


--
Robert Keller
--------------------------------------------------
robert.keller@yahoo.com


3 Answers

Phrogz

11/20/2007 6:25:00 PM

0

On Nov 20, 11:18 am, Robert Keller <rlkel...@yahoo.com> wrote:
> 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.

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.

Ben Atkin

11/20/2007 6:32:00 PM

0

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

On Nov 20, 2007 11:18 AM, Robert Keller <rlkeller@yahoo.com> wrote:
>
> 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
>
>
> --
> Robert Keller
> --------------------------------------------------
> robert.keller@yahoo.com
>
>
>



--
Ben Atkin
ben@benatkin.com
http://www.ben...

rlkeller

11/20/2007 8:30:00 PM

0

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

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&...


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&...
by Jeffrey Friedl


--
Robert Keller
--------------------------------------------------
robert.keller@yahoo.com



Ben Atkin wrote:
> 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
>
> On Nov 20, 2007 11:18 AM, Robert Keller <rlkeller@yahoo.com> wrote:
>
>> 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
>>
>>
>> --
>> Robert Keller
>> --------------------------------------------------
>> robert.keller@yahoo.com
>>
>>
>>
>>
>
>
>
>