gsub question on special charaters

R

Robert Keller

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
 
P

Phrogz

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

Ben Atkin

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

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
 
R

Robert Keller

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-Reg...bs_sr_1?ie=UTF8&s=books&qid=1195585371&sr=1-1>


Mastering Regular Expressions
<http://www.amazon.com/Mastering-Reg...bs_sr_1?ie=UTF8&s=books&qid=1195585371&sr=1-1>
by Jeffrey Friedl


--
Robert Keller
--------------------------------------------------
(e-mail address removed)



Ben said:
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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top