我在为一个代码战Katas(https://www.codewars.com/kata/parseint-reloaded/train/ruby)编写正则表达式时遇到一些麻烦,我希望可以在这里指出正确的方向。我需要一个正则表达式,可以匹配英语,人类可读的数字字符串,范围从1到999。例如:“一个”,“三百二十二”,“五百九十七”等。
当我与正则表达式匹配时,我希望这些匹配出现在一致的反向引用位置。到目前为止,我所写的内容或多或少在大多数情况下都是可行的,但后向引用遍地都是。有时当我匹配“一百”时。它涨到了3美元,其他时候涨到了6美元,这使得逻辑上难以解决。其他时间,同一字符串出现两次。有什么方法可以挽救它并使它变得更好,还是我应该忍不住为不同的情况编写多个正则表达式?
regex = "^((.+?)( hundred)? )?((.+)[ -])?(.+?)$"
test_cases = [
'seven hundred ninety six',
'six hundred twenty-two',
'one hundred',
'two hundred one',
'sixty six',
'one',
'sixty'
]
test_cases.each do |test_case|
puts test_case.match(regex).to_a.inspect
end
["seven hundred ninety six", "seven hundred ", "seven", " hundred", "ninety ", "ninety", "six"]
["six hundred twenty-two", "six hundred ", "six", " hundred", "twenty-", "twenty", "two"]
["one hundred", "one ", "one", nil, nil, nil, "hundred"]
["two hundred one", "two hundred ", "two", " hundred", nil, nil, "one"]
["sixty six", "sixty ", "sixty", nil, nil, nil, "six"]
["one", nil, nil, nil, nil, nil, "one"]
["sixty", nil, nil, nil, nil, nil, "sixty"]
答案 0 :(得分:3)
首先,构造用于将字符串转换为整数的散列,并使用这些散列的键来定义可能出现在字符串中以插入正则表达式的单词。
units_to_digit = %w| one two three four five six seven eight nine |.
zip((1..9).to_a).to_h
#=> {"one"=>1, "two"=>2, "three"=>3, "four"=>4, "five"=>5, "six"=>6, "seven"=>7,
# "eight"=>8, "nine"=>9}
units = units_to_digit.keys.join('|')
#=> "one|two|three|four|five|six|seven|eight|nine"
tens_to_digit = %w| twenty thirty forty fifty sixty seventy eighty ninety |.
zip((2..9).to_a).to_h
#=> {"twenty"=>2, "thirty"=>3, "forty"=>4, "fifty"=>5, "sixty"=>6, "seventy"=>7,
# "eighty"=>8, "ninety"=>9}
tens = tens_to_digit.keys.join('|')
#=> "twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety"
teens_to_digit =
%w| ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen |.
zip((10..19).to_a).to_h
#=> {"ten"=>10, "eleven"=>11, "twelve"=>12, "thirteen"=>13, "fourteen"=>14,
# "fifteen"=>15, "sixteen"=>16, "seventeen"=>17, "eighteen"=>18, "nineteen"=>19}
teens = teens_to_digit.keys.join('|')
#=> "ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen"
(一个人也可以为units = Regexp.union(units_to_digit.keys)
和tens
写teens
或类似内容。请参见Regexp::union。)
接下来,使用命名的捕获组构造一个正则表达式。 (出于文档目的,我使用了自由行距模式。如果不使用自由行距模式,则包含单个空格([ ]
)的字符类可以用空格替换。)
regex = /
\A # match beginning of string
(?: # begin a non-capture group
(?<nbr_hundreds>#{units}) # match nbr of hundreds, named 'nbr_hundreds'
[ ]hundred # match ' hundred'
)? # close non-capture group and make optional
[ ]? # optionally match a space
(?: # begin non-capture group
(?: # begin a non-capture group
(?<tens>#{tens}) # match 'twenty' to 'ninety', named 'tens'
(?: # begin non-capture group
[ -] # match a space or hyphen
(?<tens_units>#{units}) # match units, named 'tens_units'
)? # close non-capture group and make optional
) # close non-capture group
| # or
(?<units>#{units}) # match '1-9', named 'units'
| # or
(?<teens>#{teens}) # match 'ten', 'eleven',...'nineteen'
)? # close non-capture group and make optional
\z # match end of string
/x # free-spacing regex definition mode
#=> /
# \A
# (?:
# (?<nbr_hundreds>one|two|three|four|five|six|seven|eight|nine)
# [ ]hundred
# )?
# [ ]?
# (?:
# (?:
# (?<tens>twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety)
# (?:
# [ -]
# (?<tens_units>one|two|three|four|five|six|seven|eight|nine)
# )?
# )
# |
# (?<units>one|two|three|four|five|six|seven|eight|nine)
# |
# (?<teens>ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen)
# )?
# \z
# /x
str.match(regex)
将返回一个MatchData
对象m
。捕获组的值将为m[:nbr_hundreds]
,m[:tens]
,m[:tens_units]
,m[:units]
和m[:teens]
。如果没有匹配项,则每个将等于nil
。 (例如,m[:nbr_hundreds]
时nil
等于str = "one"
。)将这些nil视为零是很方便的。一种简单的方法是将键值对nil=>0
添加到每个哈希units_to_digit
,tens_to_digit
和teens_to_digit
:
units_to_digit[nil] = 0
tens_to_digit[nil] = 0
teens_to_digit[nil] = 0
现在构造一个将MatchData
对象转换为整数的方法。
def match_data_to_integer(units_to_digit, tens_to_digit, teens_to_digit, m)
100 * units_to_digit[m[:nbr_hundreds]] +
10 * tens_to_digit[m[:tens]] +
teens_to_digit[m[:teens]] +
units_to_digit[m[:tens_units]] +
units_to_digit[m[:units]]
end
现在让我们针对一些字符串进行测试。
test_cases = [
'seven hundred ninety six',
'six hundred twenty-two',
'one hundred',
'two hundred one',
'sixty six',
'one',
'sixty'
]
test_cases.each do |test_case|
m = test_case.match(regex)
n = match_data_to_integer(units_to_digit, tens_to_digit, teens_to_digit, m)
puts "#{test_case} -> #{n}"
end
打印
seven hundred ninety six -> 796
six hundred twenty-two -> 622
one hundred -> 100
two hundred one -> 201
sixty six -> 66
one -> 1
sixty -> 60