String extraction

Hi,

I have strings of the form:

string='.///root/1068663688//1068824962/1068836207//1068932537///'

Basically, I want to extract the last number in the string.

Right now I'm doing:

string[/(\d*)\/*$/].delete('/')

But I've already matched the digit I want, so the delete is just
superfluous work. How can I extract the number without doing a
separate delete?

Thanks,
Navin.

Navindra Umanee wrote:

Hi,

I have strings of the form:

string='.///root/1068663688//1068824962/1068836207//1068932537///'

Basically, I want to extract the last number in the string.

Right now I'm doing:

string[/(\d*)\/*$/].delete('/')

But I've already matched the digit I want, so the delete is just
superfluous work. How can I extract the number without doing a
separate delete?

Thanks,
Navin.

class String
  def str_replace(what,with)
    re = Regexp.new(Regexp.quote(what))
    self.gsub(re) {with}
  end
   def str_ireplace(what,with)
    re = Regexp.new(Regexp.quote(what), Regexp::IGNORECASE)
    self.gsub(re) {with}
  end
end

string='.///root/1068663688//1068824962/1068836207//1068932537///'

string = string.str_replace('///','')
string = string.str_replace('//','/')
temp = string.split('/')
return temp[4]

regards
Eko

string[%r|/(\d+)/+$|, 1] ?

hth,

-Martin

···

On Sat, Jan 29, 2005 at 04:05:57PM +0900, Navindra Umanee wrote:

Hi,

I have strings of the form:

string='.///root/1068663688//1068824962/1068836207//1068932537///'

Basically, I want to extract the last number in the string.

Hi --

···

On Sat, 29 Jan 2005, Navindra Umanee wrote:

Hi,

I have strings of the form:

string='.///root/1068663688//1068824962/1068836207//1068932537///'

Basically, I want to extract the last number in the string.

Right now I'm doing:

string[/(\d*)\/*$/].delete('/')

But I've already matched the digit I want, so the delete is just
superfluous work. How can I extract the number without doing a
separate delete?

You can use a negative-width lookahead:

   string[/(\d*)(?=\/*$)/]

David

--
David A. Black
dblack@wobblini.net

Hi,

···

Am Samstag, 29. Jan 2005, 16:05:57 +0900 schrieb Navindra Umanee:

I have strings of the form:

string='.///root/1068663688//1068824962/1068836207//1068932537///'

Basically, I want to extract the last number in the string.

string[/(\d*)\/*$/].delete('/')

But I've already matched the digit I want, so the delete is just
superfluous work. How can I extract the number without doing a
separate delete?

Maybe you simply need to say

  string[/\d+\/*$/].to_i

that doesn't mind trailing slashes.

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

Hi -

···

On Sat, 29 Jan 2005, David A. Black wrote:

Hi --

On Sat, 29 Jan 2005, Navindra Umanee wrote:

Hi,

I have strings of the form:

string='.///root/1068663688//1068824962/1068836207//1068932537///'

Basically, I want to extract the last number in the string.

Right now I'm doing:

string[/(\d*)\/*$/].delete('/')

But I've already matched the digit I want, so the delete is just
superfluous work. How can I extract the number without doing a
separate delete?

You can use a negative-width lookahead:

Sorry, too little coffee. That's zero-width lookahead :slight_smile:

David

--
David A. Black
dblack@wobblini.net

Exactly what I was looking for, thanks. The zero-width positive
lookahead is also interesting.

str[regexp, fixnum]

Is there a similar way to combine and use different matched groups
instead of being restricted to a fixnum? Say instead of just \1 you
had a match for \1 and \2 and you wanted the output string to be \2 \1

"byru"[/(by)(ru)/, \2\1]

should output "ruby".

Thanks,
Navin.

···

Martin S. Weber <Ephaeton@gmx.net> wrote:

string[%r|/(\d+)/+$|, 1] ?