silly regex question

J

Joe Van Dyk

Can someone help me make this code not suck?

require 'test/unit'

# EWWWWWWW
def process_string str
result =3D Hash.new
str.to_a.each do |line|
line.scan(/important ([a-zA-Z0-9]+): /) do |key|
result[key.first] =3D 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 =3D 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
 
J

James Edward Gray II

Can someone help me make this code not suck?

I guess it depends on what you mean by that...
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

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

James Edward Gray II
 
J

Joe Van Dyk

Can someone help me make this code not suck?

I guess it depends on what you mean by that...
require 'test/unit'

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

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

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

James Edward Gray II

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
 
J

Joe Van Dyk

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
 
W

Wilson Bilkovich

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 o=
ption
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
 
D

Dan Kohn

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",
:eek:n => :create
 
J

Joe Van Dyk

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

ah, nice. Too bad vim doesn't highlight the comments. :(
 
C

Chad Perrin

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.
 
D

Devin Mullins

Joe said:
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
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.
 
J

Jeffrey Moss

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
 
B

Brian Caswell

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
 

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
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top