Howto Delete 3 Leftmost Characters

i’m sure this is easy, but i’ve gone through Pickaxe’s string methods,
searched the web and searched the groups, and i can’t figure out how to
do this in Ruby.

i grep a file and it returns the following string:

#ip 127.0.0.1

i now want to get rid of “#ip” so i can then strip the remaining string
to get rid of spaces.

however, i can’t find out how to delete the 3 leftmost characters - in
this case “#ip”.

thanks for any tips to get this done - i would think it is a matter of
just knowing the correct method.

Skeets wrote:

however, i can’t find out how to delete the 3 leftmost characters - in
this case “#ip”.

thanks for any tips to get this done - i would think it is a matter of
just knowing the correct method.

irb(main):001:0> s="#ip 127.0.0.1"
=> “#ip 127.0.0.1”
irb(main):002:0> s.slice!(0,3)
=> “#ip
irb(main):003:0> s
=> " 127.0.0.1"

Skeets wrote:

however, i can’t find out how to delete the 3 leftmost characters - in
this case “#ip”.

thanks for any tips to get this done - i would think it is a matter of
just knowing the correct method.

If you do the grepping in Ruby you can do something like this:

ip = nil

File.foreach(“foo.txt”) do |line|
ip = $1 if /^#ip\s+(\d+(?:.\d+){3})/ =~ line
end

HTH

robert

On Mon, Jul 31, 2006 at 06:30:13AM +0900, Skeets wrote:

however, i can’t find out how to delete the 3 leftmost characters - in
this case “#ip”.

"#ip 127.0.0.1"[3..-1] # => " 127.0.0.1"
"#ip 127.0.0.1"[4..-1] # => "127.0.0.1"

But you’re probably better off using regular expressions instead of
fixed
indices:

"#ip 127.0.0.1".sub(/^#ip\s+/, '') # => "127.0.0.1"

That is, “remove, from the beginning of the line, ‘#ip’ followed by one
or
more space characters (be them spaces, tabs or whatever)”. If you don’t
know
regular expressions, and you’re into text processing, I recommend you
to go
and read some book about regular expressions and practice a little
(under
Linux there are a couple of handy utilities for that; I’m sure there
must be
also for other platforms).

Regards,

William J. wrote:

to get rid of spaces.
=> “#ip
irb(main):003:0> s
=> " 127.0.0.1"

William, thanks. when i follow your approach, i get your result via
irb. however, i get a different result when i run code in a file. i’m
doing something different, but i don’t know what.

here is the code:

#!/usr/bin/env ruby

if File.exist?( “ip.txt” )

f = File.open("ip.txt’).grep(/#ip/)
(f.to_s).slice!(0,3)

f = f.strip

end

puts f # this outputs “#ip 127.0.0.1” - i was expecting it to output
“127.0.0.1”

if i have

f = (f.to_s).slice!(0,3)

instead fo

(f.to_s).slice!(0,3)

then “puts f” prints “#ip

tia…

On Mon, Jul 31, 2006 at 06:51:35AM +0900, Esteban Manchado Velázquez
wrote:

Linux there are a couple of handy utilities for that; I’m sure there must be
also for other platforms).

Actually, the best text I’ve ever had the pleasure to read for learning
regex syntax from scratch was Learning Perl. The downside is that you
kinda have to know some Perl for it to make much sense (or go through
the entire book and learn regular expressions as a part of learning
Perl) – which is only a downside if you don’t want to learn Perl, of
course. It’s a reasonably easy book to work through, though, and great
at what it does. All else being equal, it’s better to know more
languages anyway.

If there are some really good materials for regexen that are specific to
Ruby, of course, that may be more what you need – but I don’t know of
any off the top of my head. Obviously, several of the available e-books
and tutorials you can find online address regex usage, but as far as
I’ve seen they don’t tend to do as good a job of it as Learning Perl.

Hi –

On Mon, 31 Jul 2006, Skeets wrote:

i now want to get rid of “#ip” so i can then strip the remaining string
irb(main):002:0> s.slice!(0,3)
#!/usr/bin/env ruby
“127.0.0.1”

if i have

f = (f.to_s).slice!(0,3)

instead fo

(f.to_s).slice!(0,3)

then “puts f” prints “#ip

The reason is that when you do this:

f.to_s.slice! …

you’re operating on a string object other than f. Ruby first does
f.to_s, which returns a new string, then does an in-place slice!
operation on that string.

In the f = … example, you’re reassigning to f, so now f is the new,
modified string.

David

Esteban Manchado Velázquez wrote:

to get rid of spaces.
#ip 127.0.0.1”.sub(/^#ip\s+/, ‘’) # => “127.0.0.1”

Esteban Manchado Velázquez [email protected] - http://www.foton.es

Esteban, and all - thank you.

this did the trick…

if File.exist?( ‘current_ip.txt’ )

f = File.open(‘current_ip.txt’).grep(/#ip/)
f = f[0].sub(/^#ip\s+/, ‘’)

end

puts f

the File.open grep line returned an array. i had to sort that out
first. i know this file will always have only one instance of #ip - is
there any way to force grep to return as a variable instead of an
array?

also, i believe the regex Esteban gave will get rid of all white spaces

  • both to the left and right of the ip address.

thanks to everyone for the help.

Skeets wrote:

i now want to get rid of “#ip” so i can then strip the remaining string

f = f[0].sub(/^#ip\s+/, ‘’)
also, i believe the regex Esteban gave will get rid of all white spaces

  • both to the left and right of the ip address.

thanks to everyone for the help.

file_name = ‘current_ip.txt’
if File.exist?( file_name )
ip = File.read( file_name )[ /^#ip\s+([\d.]+)/, 1 ]
end
p ip

also, i believe the regex Esteban gave will get rid of all white spaces

  • both to the left and right of the ip address.

actually, this isn’t true so i still have to use the strip method.

i have some regex experience - but not too much.

i pretty much take some pain killers and then try and figure out what i
need as the need arises. :wink:

i’ll bet this regex can be update to remove all spaces, however, i will
have to look into that next week.

thanks to everyone for getting me back on track.

i’m working on my first ruby program - and i’m impressed with ruby.

lots to learn, though.

Skeets schrieb:

however, i can’t find out how to delete the 3 leftmost characters - in
this case “#ip”.

thanks for any tips to get this done - i would think it is a matter of
just knowing the correct method.

irb(main):022:0> “abcdeg”.sub(/…/, “”)
=> “deg”
irb(main):023:0> “abcdeg”[3…-1]
=> “deg”
irb(main):024:0>

hth, Daniel

Esteban Manchado =?iso-8859-1?Q?Vel=E1zquez?= wrote:

…snip…
That is, “remove, from the beginning of the line, ‘#ip’ followed by one
or
more space characters (be them spaces, tabs or whatever)”.>
…snip…
Regards,

Reading this explanation of the regexp that was provided reminded me
again of a question that has been haunting me for awhile. Has anyone
written an “English language to Regexp” converter?

It seems to me that for relatively simple cases it should be relatively
easy to set up something similar to the way most email apps do rule
building for filtering messages, with a drop down list of "contains,
starts with, doesn’t contain, ends with, …) and a couple of buttons
for adding/removing another line of such stuff. Sort of a wizard for
building RegExp.

Unless you write two or three RegExp’s a day, it seems nearly impossible
to remember this arcane but useful syntax. Being able to write them in
plain English or build one with some simple drop downs and check boxes
etc. would certainly save me some pain on occasion. I’ve searched for
something like this before but never found anything.

jp

Jeff P. wrote:

Reading this explanation of the regexp that was provided reminded me
Unless you write two or three RegExp’s a day, it seems nearly impossible
to remember this arcane but useful syntax. Being able to write them in
plain English or build one with some simple drop downs and check boxes
etc. would certainly save me some pain on occasion. I’ve searched for
something like this before but never found anything.

jp

Sounds like a Ruby Q. to me!

However, I’ve used a variety of these GUI-based regex builders in the
past and never found them to much more than a curiosity. The regular
expression syntax is so precise and exact that I find it frustrating to
have to pick through all the choices and click things. Invariably I end
up just writing the damn regex by hand. I would think that an English
language equivalent to regex syntax would simply replace
hard-to-remember regular expressions with hard-to-remember English
phrases.

Regular expressions are no harder to learn than any other programming
language. You need a good reference (for me it has to be on paper) and
some practice. That’s all.

Schüle Daniel wrote:

to get rid of spaces.
irb(main):023:0> “abcdeg”[3…-1]
=> “deg”
irb(main):024:0>

hth, Daniel

thanks everyone - this whole discussion has been enlightening for this
newb.

Hi –

On Mon, 31 Jul 2006, Jeff P. wrote:

written an “English language to Regexp” converter?
Yes, Florian G. has, though I don’t know how complete it is (this
was a couple of years ago and I haven’t heard much about it since
then). It’s called Regexp::English. I can’t seem to find it
anywhere… paging Florian…

David

On Mon, Jul 31, 2006 at 02:18:36PM +0900, Martin DeMello wrote:

s[/^#ip /] = ‘’
That changes the behavior of the regex, though. Maybe more like:

s = “#ip 127.0.0.1”
s[/^#ip\s+/] = ‘’

. . . unless we’re sure there will always be exactly one space (which is
the case if ‘s = “#ip 127.0.0.1”’ is the literal code we’re using), in
which case the literal space in the matching regex is appropriate.

On Mon, Jul 31, 2006 at 07:25:04AM +0900, Skeets wrote:

i’ll bet this regex can be update to remove all spaces, however, i will
have to look into that next week.

Something like:

s = "#ip   127.0.0.1    "
s.sub(/^#ip\s+(.+?)\s*$/, '\1')

That’s way uglier, because you have to match the whole line and then
substitute everything for the first parens contents… and you have
to use
“.+?” instead of simply “.+” inside them, or otherwise you will get
everything
until the end of the line :-/

The "?" after "*" or "+" means non-greediness, BTW.

On 7/31/06, Esteban Manchado Velázquez [email protected] wrote:

But you’re probably better off using regular expressions instead of fixed
indices:

"#ip 127.0.0.1".sub(/^#ip\s+/, '') # => "127.0.0.1"

A prettier looking form, in this case, is

s = “#ip 127.0.0.1”
s[/^#ip /] = ‘’

martin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs