Hopefully simple regex

S

stephen O'D

Hi,

I am trying to make a regex that will capture strings that look like
'@string' surrounded by whitespace or at the start or end of a line -
here is the spec:


require 'test/unit'

class RegexTest < Test::Unit::TestCase

def setup
# this doesn't work correctly for the fails scenarios
@regex = /(@{1,1}\w+)/
end

def test_regex_matches
str = [
"@bar some more text",
"@bar",
"sometext @bar text",
"sometext @bar"
]
str.each do |s|
s =~ @regex
assert_equal('@bar', $1)
end
end

def test_regex_fails
str = [
"@@bar",
"sometext @@bar text",
"sometext@bar text",
"sometext@bar"
]
str.each do |s|
s =~ @regex
assert_equal(nil, $1)
end
end

end

I have tried all sorts of combinations of character class and negated
classes etc until my eyes have started glazing over - I really though
this should be easy - can anyone suggest a regex that pass my spec
above?

Thanks,

Stephen.
 
D

David A. Black

Hi --

Hi,

I am trying to make a regex that will capture strings that look like
'@string' surrounded by whitespace or at the start or end of a line -
here is the spec:


require 'test/unit'

class RegexTest < Test::Unit::TestCase

def setup
# this doesn't work correctly for the fails scenarios
@regex = /(@{1,1}\w+)/
end

I have tried all sorts of combinations of character class and negated
classes etc until my eyes have started glazing over - I really though
this should be easy - can anyone suggest a regex that pass my spec
above?

Try this:

@regex = /(?:^|\s)(@\w+)/


David
 
S

stephen O'D

Hi,

I am trying to make a regex that will capture strings that look like
'@string' surrounded by whitespace or at the start or end of a line -
here is the spec:

require 'test/unit'

class RegexTest < Test::Unit::TestCase

def setup
# this doesn't work correctly for the fails scenarios
@regex = /(@{1,1}\w+)/
end

def test_regex_matches
str = [
"@bar some more text",
"@bar",
"sometext @bar text",
"sometext @bar"
]
str.each do |s|
s =~ @regex
assert_equal('@bar', $1)
end
end

def test_regex_fails
str = [
"@@bar",
"sometext @@bar text",
"sometext@bar text",
"sometext@bar"
]
str.each do |s|
s =~ @regex
assert_equal(nil, $1)
end
end

end

I have tried all sorts of combinations of character class and negated
classes etc until my eyes have started glazing over - I really though
this should be easy - can anyone suggest a regex that pass my spec
above?

Thanks,

Stephen.

It seems like this regex works:

/^(?:.*\s+(@{1,1}\w+)|(@{1,1}\w+))/

Although you have to change the spec a little to check $1 and $2 for
matches. Still seems a bit over complex to me - any better
suggestions?
 
S

stephen O'D

Try this:

@regex = /(?:^|\s)(@\w+)/

David

Perfect - I didn't know you could (or even think to try) putting the
'start of string' anchor inside a group with an alternative! At least
I have learned something from the last hour of trying!
 
R

Richard Conroy

Perfect - I didn't know you could (or even think to try) putting the
'start of string' anchor inside a group with an alternative! At least
I have learned something from the last hour of trying!

http://www.rubular.com is an awesome way of composing and testing ruby
regular expressions. Highly recommended.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top