/* 最新版本请参见 http://www.blogjava.net/chelsea/archive/2007/12/16/168118.html */
找了半天也没有找到创建正则表达式的DSL, 如果你知道请告诉我
只好先自己写来应急了:
http://roll-stone.googlecode.com/svn/trunk/RegexBuilder/
用起来类似这样:
require
'regex_builder'
class
RegexBuilderTest < Test::Unit::TestCase
def
setup
@rb
= RegexBuilder.
new
end
def
test_ruby_start_with_r
pattern =
@rb
.start_with(
"r"
).to_s
assert
"ruby"
.match(pattern)
end
def
test_ruby_start_with_r_and_end_with_y
pattern =
@rb
.start_with(
"r"
).any().end_with(
"y"
).to_s
assert
"ruby"
.match(pattern)
end
end
还有一个 RegexUtil 来直接提供常用的正则表达式, 如email地址,ip地址等,想到啥都可以往里加
require
'regex_util'
class
RegexUtilTest < Test::Unit::TestCase
def
test_192_168_0_3_is_a_valid_ip_address
assert
"192.168.0.3"
.match(RegexUtil::IP_Pattern)
end
def
test_500_312_0_3_is_not_a_valid_ip_address
assert_nil(
"500.312.0.3"
.match(RegexUtil::IP_Pattern)
)
end
def
test_chelsea_at_gmail_dot_com_is_a_valid_email_address
assert
"chelsea@gmail.com"
.match(RegexUtil::Email_Pattern)
end
def
test_www_dot_google_dot_com_is_not_a_valid_email_address
assert_nil(
"www.google.com"
.match(RegexUtil::Email_Pattern)
)
end
end
很多功能都还没提供,希望有人一起来做