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