All-
I’d appreciate your help on this one:
Please advise as to how I can copy a substring X from string Y starting with string Z through the end of string Y.
For example, supposing I have these strings:
Y = "The quick brown fox jumped over the lazy dogs back."
Z = “jumped”
I need a routine that will assign to string X the value
“jumped over the lazy dogs back.”
I’ll be putting this code in a script that will process several string Ys consecutively, where string Z is known to occur in each string Y.
Thanks!
-Kurt
Hi –
All-
I’d appreciate your help on this one:
Please advise as to how I can copy a substring X from string Y starting with string Z through the end of string Y.
For example, supposing I have these strings:
Y = “The quick brown fox jumped over the lazy dogs back.”
Z = “jumped”
I need a routine that will assign to string X the value
“jumped over the lazy dogs back.”
I’ll be putting this code in a script that will process several
string Ys consecutively, where string Z is known to occur in each
string Y.
Give or take multiline variants, etc., you could do:
/#{x}.*/.match(y)[0] # =>“jumped over the lazy dogs back.”
To make sure that any regex special characters in x don’t get
used as special characters, you could do:
s = Regexp.escape(x)
/#{s}.*/.match(y)[0]
David
···
On Thu, 1 Aug 2002, Kurt Euler wrote:
–
David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav
x = y.slice(Regexp.new(“#{z}.*$”))
There might be a faster way to code it, but this works.
···
On Thu, Aug 01, 2002 at 06:58:23AM +0900, Kurt Euler wrote:
All-
I’d appreciate your help on this one:
Please advise as to how I can copy a substring X from string Y starting with string Z through the end of string Y.
For example, supposing I have these strings:
Y = “The quick brown fox jumped over the lazy dogs back.”
Z = “jumped”
I need a routine that will assign to string X the value
“jumped over the lazy dogs back.”
I’ll be putting this code in a script that will process several string Ys consecutively, where string Z is known to occur in each string Y.
Thanks!
-Kurt
–
Alan Chen
Digikata LLC
http://digikata.com
[…]
Please advise as to how I can copy a substring X from string Y
starting with string Z through the end of string Y.
Yet another:
x = y[(y =~ /#{z}/)…-1]
Does not work reliably if ‘z’ contains special regular expression
characters such as ‘.’:
irb(main):001:0> z = “.”
“.”
irb(main):002:0> y = “alpha.beta.”
“alpha.beta.”
irb(main):003:0> x = y[(y =~ /#{z}/)…-1]
“alpha.beta.”
You want to use String#index, e.g.
x = y[y.index(z)…-1]
Reimer Behrends
···
Mauricio Fernández (batsman.geo@yahoo.com) wrote:
On Thu, Aug 01, 2002 at 06:58:23AM +0900, Kurt Euler wrote: