Forum: Ruby silly regex question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2006-01-03 23:24
(Received via mailing list)
Can someone help me make this code not suck?

require 'test/unit'

# EWWWWWWW
def process_string str
  result = Hash.new
  str.to_a.each do |line|
    line.scan(/important ([a-zA-Z0-9]+): /) do |key|
      result[key.first] = line
    end
  end
  result
end

class TestThis < Test::Unit::TestCase
  def example_string
    <<-END
    # comments
    important key1: some value's here
    important key2: some value's here
    important key3: some value's here
    important key4: some value's here

    other stuff we don't care about
    END
  end

  def test_process_string
    result = process_string example_string
    assert 4, result.size
    assert result.has_key?("key1")
    assert result.has_key?("key2")
    assert result.has_key?("key3")
    assert result.has_key?("key4")
  end
end
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-01-03 23:36
(Received via mailing list)
On Jan 3, 2006, at 4:22 PM, Joe Van Dyk wrote:

> Can someone help me make this code not suck?

I guess it depends on what you mean by that...

>   result
> end

def process_string str
   Hash[*str.scan(/^\s*(important ([a-zA-Z0-9]+): .+)
$/).flatten.reverse]
end

James Edward Gray II
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2006-01-03 23:42
(Received via mailing list)
On 1/3/06, James Edward Gray II <james@grayproductions.net> wrote:
> >   result = Hash.new
> $/).flatten.reverse]
> end

IMO, my version's more readable.  I'm going for readability here.
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-01-03 23:54
(Received via mailing list)
On Jan 3, 2006, at 4:41 PM, Joe Van Dyk wrote:

> IMO, my version's more readable.  I'm going for readability here.

def process_string str
   result = Hash.new
   str.scan(/^\s*(important ([a-zA-Z0-9]+): .+?)\s*$/) do |line, key|
     result[key] = line
   end
   result
end

# ... or ...

def process_string str
   str.inject(Hash.new) do |result, line|
     result[$1] = line if line =~ /^\s*important ([^:]+):/
     result
   end
end

James Edward Gray II
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2006-01-04 00:17
(Received via mailing list)
Another silly regex question.

I have a regex that's getting to be more than 100 chars long.  How can
I split it up on multiple lines?

Joe
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-01-04 00:26
(Received via mailing list)
On 1/3/06, Joe Van Dyk <joevandyk@gmail.com> wrote:
> Another silly regex question.
>
> I have a regex that's getting to be more than 100 chars long.  How can
> I split it up on multiple lines?
>
You can put the 'x' option on the end of the regular expression.
From the Pickaxe:
ExtendedMode: Complex regular expressions can be difficult to read. The
x option
allows you to insert spaces, newlines, and comments in the pattern to
make it more
readable.

e.g.
%r{some regex
with
multiple
lines
}x
5730f209b34b8474639e0c2020f54513?d=identicon&s=25 Dan Kohn (Guest)
on 2006-01-04 00:29
(Received via mailing list)
Here's a rails example for validating email addresses.

  validates_format_of :login, :with => /
    ^[-^!$#%&'*+\/=?`{|}~.\w]+
    @[a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9])*
    (\.[a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9])*)+$/x,
    :message => "must be a valid email address",
    :on => :create
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2006-01-04 00:42
(Received via mailing list)
On 1/3/06, Wilson Bilkovich <wilsonb@gmail.com> wrote:
> make it more
> readable.
>
> e.g.
> %r{some regex
> with
> multiple
> lines
> }x

ah, nice.  Too bad vim doesn't highlight the comments.  :(
Fd22ee3cfc7dac283ce8e451af324f7d?d=identicon&s=25 Chad Perrin (Guest)
on 2006-01-04 01:44
(Received via mailing list)
On Wed, Jan 04, 2006 at 07:51:55AM +0900, James Edward Gray II wrote:
>
> def process_string str
>   str.inject(Hash.new) do |result, line|
>     result[$1] = line if line =~ /^\s*important ([^:]+):/
>     result
>   end
> end

Not that I'm the one that has to read it, but . . .
I like this one for readability.

--
Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ]

unix virus: If you're using a unixlike OS, please forward
this to 20 others and erase your system partition.
Fd22ee3cfc7dac283ce8e451af324f7d?d=identicon&s=25 Chad Perrin (Guest)
on 2006-01-04 01:44
(Received via mailing list)
On Wed, Jan 04, 2006 at 08:40:43AM +0900, Joe Van Dyk wrote:
>
> ah, nice.  Too bad vim doesn't highlight the comments.  :(

Have you tried setting this?:

  :syntax on

--
Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ]

This sig for rent:  a Signify v1.14 production from
http://www.debian.org/
918c6daad03c85e51ad1a11f57017947?d=identicon&s=25 Devin Mullins (Guest)
on 2006-01-04 05:49
(Received via mailing list)
Joe Van Dyk wrote:

>    end
>  end
>  result
>end
>
>
def process_string str
  result = {}
  str.each_line do |line|
    important_stuff = line.split(/important\s+/,2)[1] or next
    key, value = important_stuff.split ':'
    result[key] = value
  end
  result
end
# More readable? I dunno... you be the judge. I might prefer it over:
# line, key, value = line.match /important\s+(\w+):\s*(.*)$/
# Though the latter is much more explicit.
6979e3d7e89e66d34d9a767e4d9a07bf?d=identicon&s=25 Jeffrey Moss (Guest)
on 2006-01-04 13:27
(Received via mailing list)
I was just perfecting my email address validator, mine allows multiple
email addresses in the email_address field:

validates_format_of :email_address, :with =>
/^\s*(?:(?:[^,@\s]+)@(?:(?:[-a-z0-9]+\.)+[a-z]{2,}\s*(,\s*|\z)))+$/i,
:allow_nil => true

It doesn't do as careful of an inspection as yours, although I've seen
some validations that are far more detailed. It'd be nice to have some
people contribute their suggestions for the ultimate email address
validation regular expression.

I also have this handy method in my class:

  def email_addresses
    self.email_address.split(',').map{|a| a.lstrip.rstrip }
  end

-Jeff
Bda9bdaf350ff0f6c75618bbb69b76d3?d=identicon&s=25 Brian Caswell (Guest)
on 2006-01-04 13:55
(Received via mailing list)
On Jan 4, 2006, at 7:25 AM, Jeffrey Moss wrote:
> It doesn't do as careful of an inspection as yours, although I've seen
> some validations that are far more detailed. It'd be nice to have some
> people contribute their suggestions for the ultimate email address
> validation regular expression.

It already exists.  Go buy the first edition of Mastering Regular
Expressions.  There 11 page regex that matches emails.

Brian
Fee23d1fc58edee59e05d7a52dcf172e?d=identicon&s=25 Kevin Brown (Guest)
on 2006-01-04 18:59
(Received via mailing list)
On Wednesday 04 January 2006 06:25, Jeffrey Moss wrote:
> people contribute their suggestions for the ultimate email address
> validation regular expression.
>
> I also have this handy method in my class:
>
>   def email_addresses
>     self.email_address.split(',').map{|a| a.lstrip.rstrip }
>   end

Why do you lstrip and then rstrip?  Won't a simple strip work for you?
6979e3d7e89e66d34d9a767e4d9a07bf?d=identicon&s=25 Jeffrey Moss (Guest)
on 2006-01-04 22:07
(Received via mailing list)
> >   def email_addresses
> >     self.email_address.split(',').map{|a| a.lstrip.rstrip }
> >   end
>
> Why do you lstrip and then rstrip?  Won't a simple strip work for you?

YES! Nice to know! Hehe.

-Jeff
This topic is locked and can not be replied to.