Chopping the beginning of a string elegantly

F

francisrammeloo

Hi all,

I'm having some trouble finding an elegant way of removing a part of a
string based on an pattern.

Suppose I want to chop off the "Begin" in the string "BeginCaption".
My solution is this:

thePane = widgetdescription.scan( /Begin\w+/ )[0]
thePane = widgetdescription.["Begin".length..thePane.length]

Personally I don't find this method very elegant.

Can anybody offer me some hints on how to make this code more
presentable?

Also perhaps a similar strategy if I would want to remove something
from the middle or the end?

Best regards,
Francis
 
J

Jason Voegele

Suppose I want to chop off the "Begin" in the string "BeginCaption".
My solution is this:

thePane =3D widgetdescription.scan( /Begin\w+/ )[0]
thePane =3D widgetdescription.["Begin".length..thePane.length]

Personally I don't find this method very elegant.

Can anybody offer me some hints on how to make this code more
presentable?

Use the sub method:

widget_description =3D "BeginCaption"
the_pane =3D widget_description.sub(/^Begin/, "")

The '^' in the regular expression above is an anchor to the beginning of
the string.
Also perhaps a similar strategy if I would want to remove something
from the middle or the end?

Just leave the '^' out of the regular expression from above. If you want
to remove at the end, put a '$' at the end of your regular expression.

--=20
Jason Voegele
"There is an essential core at the center of each man and woman that
remains unaltered no matter how life's externals may be transformed
or recombined. But it's smaller than we think."
-- Gene Wolfe, The Book of the Long Sun
 
K

Kero

I'm having some trouble finding an elegant way of removing a part of a
string based on an pattern.

str = "BeginOfString"
str[/Begin/] => "Begin"
str[/Of/] => "Of"

if you want to restrict it to the beginning, do

str[/^Begin/] => "Begin"
str[/^Of/] => nil

and then for replacing it, or removing it

str[/^Begin/] = "" => ""
str => "OfString"
str[/Of/] = "a" => "a"
str => "aString"

For lots more of information on patterns, read the regexp chapter of the
pickaxe.

hth,
Kero.

+--- Kero ------------------------- kero@chello@nl ---+
| all the meaningless and empty words I spoke |
| Promises -- The Cranberries |
+--- M38c --- http://members.chello.nl/k.vangelder ---+
 
W

Wybo Dekker

Hi all,

I'm having some trouble finding an elegant way of removing a part of a
string based on an pattern.

Suppose I want to chop off the "Begin" in the string "BeginCaption".
irb(main):001:0> w='BeginCaption'
=> "BeginCaption"
irb(main):002:0> w.slice!(/^Begin/)
=> "Begin"
irb(main):003:0> w
=> "Caption"
 
W

Wybo Dekker

Wybo said:
irb(main):001:0> w='BeginCaption'
=> "BeginCaption"
irb(main):002:0> w.slice!(/^Begin/)
=> "Begin"
irb(main):003:0> w
=> "Caption"
but what you asked was perhaps more like this:
irb(main):004:0> widgetdescription='BeginCaption'
=> "BeginCaption"
irb(main):005:0> (thePane=widgetdescription).slice!(/^Begin/)
=> "Begin"
irb(main):006:0> thePane
=> "Caption"

if there is no match:
irb(main):007:0> widgetdescription='EndCaption'
=> "EndCaption"
irb(main):008:0> (thePane=widgetdescription).slice!(/^Begin/) or warn
"no match"
no match
=> nil
irb(main):009:0> thePane
=> "EndCaption"

or perhaps:
irb(main):010:0> thePane=/^Begin(.*)/.match(widgetdescription)[1]
=> "Caption"
(but that goes wrong if there is no ^Begin)
 
J

Jason Voegele

Hi all,

I'm having some trouble finding an elegant way of removing a part of a
string based on an pattern.

Suppose I want to chop off the "Begin" in the string "BeginCaption".
[snip]

With all the different responses you've received, it's obvious that the
Perl mantra "There's More Than One Way To Do It" also holds true with
Ruby. :)

--=20
Jason Voegele
"There is an essential core at the center of each man and woman that
remains unaltered no matter how life's externals may be transformed
or recombined. But it's smaller than we think."
-- Gene Wolfe, The Book of the Long Sun
 
F

francisrammeloo

Thanks all,

I eventually used this

thePane = line.scan(/Begin\w+/)[0].sub(/Begin/, "")

where line looks like this:
BeginCaption 0, 20, 66, ..

It looks a little better but still not very elegant. Is it possible to
make it even still shorter?

Best regards,
Francis
 
B

Brian Schröder

Thanks all,
=20
I eventually used this
=20
thePane =3D line.scan(/Begin\w+/)[0].sub(/Begin/, "")
=20
where line looks like this:
BeginCaption 0, 20, 66, ..
=20
It looks a little better but still not very elegant. Is it possible to
make it even still shorter?
=20
Best regards,
Francis
=20
=20
=20

line.sub(/^Begin(\w+).*/, '\1')
=3D> "Caption"


--=20
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/
 
W

William James

Thanks all,

I eventually used this

thePane = line.scan(/Begin\w+/)[0].sub(/Begin/, "")

where line looks like this:
BeginCaption 0, 20, 66, ..

It looks a little better but still not very elegant. Is it possible to
make it even still shorter?

line[/Begin(\w+)/]; thePane = $1
 
D

David Vallner

William said:
(e-mail address removed) wrote:

Thanks all,

I eventually used this

thePane = line.scan(/Begin\w+/)[0].sub(/Begin/, "")

where line looks like this:
BeginCaption 0, 20, 66, ..

It looks a little better but still not very elegant. Is it possible to
make it even still shorter?

line[/Begin(\w+)/]; thePane = $1
thePane = line.sub(/Begin/, "") should work. Look, mom, nothing gets
clobbered in place, no magic globals are used.

Just my 2 cents...

David
(as likkle a newbie as they get)
 
F

francisrammeloo

I'm sorry but my first post was misleading.

The string actually looks like this:

" BeginCaption 0, 20, 66, .."
(the whitespace can be a combination of tabs and spaces)

So I actually have to cut out "Caption" word in the <<middle>> of the
string.

Brian's hint got me on the right track. The solution I finally found
was:

pane = line.sub(/\s*Begin(\w+).*/, '\1')

Unless some expert can prove a still more tight solution I believe I
have found the best way to cut out the "Caption" part of the string.

Thank you all for your help.

Best regards,
Francis
 
D

david

Cit=E1t "[email protected] said:
I'm sorry but my first post was misleading.
=20
The string actually looks like this:
=20
" BeginCaption 0, 20, 66, .."
(the whitespace can be a combination of tabs and spaces)
=20
So I actually have to cut out "Caption" word in the <<middle>> of the
string.
=20
Brian's hint got me on the right track. The solution I finally found
was:
=20
pane =3D line.sub(/\s*Begin(\w+).*/, '\1')
=20
Unless some expert can prove a still more tight solution I believe I
have found the best way to cut out the "Caption" part of the string.
=20
Thank you all for your help.
=20
Best regards,
Francis
=20
=20
=20


Hmm. A wild guess (e.g. until there's a Windows installer for Ruby 1.9 w/
Oniguruma, or someone makes a SuSE compatible RPM for that) is that lookb=
ehinds
in REs could reduce that to:

pane =3D line[/(?<=3DBegin)\w+/]

There's a 95% chance you don't need those groups to catch the initial whi=
tespace
and consume the rest of the string, so:

pane =3D line.match(/Begin(\w+)/)[1]

also works and has a bit less noise. Weirdly though, Benchmark tells me i=
t's
slightly slower. Guess you can't have your cake and eat it.

David
(ikkle newbie and all that)
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top