描述
此正则表达式将匹配字符串中的数字,前提是数字被空格或符号包围。
(?<=\s|^)(?:[0-9]+|one|two|three|four|five|six|seven|eight|nine|ten)(?=\s|$)
现场示例:http ://www.rubular.com/r/6ua7fTb8IS
要包含一到十之外的数字的拼写单词版本,您需要包含这些数字。此正则表达式将捕获从零到一百的数字 [排除任何错别字]
(?<=\s|^)(?:[0-9]+|(?:(?:twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety)\s)?(?:one(?:[\s-]hundred)?|two|three|four|five|six|seven|eight|nine)|ten|eleven|twelve|(?:thir|four|fif|six|seven|eight|nine)teen|twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety|zero)(?=\s|$)
现场示例:http ://www.rubular.com/r/EIa18nx731
Perl 示例
$string = <<END;
... 1 child ...
... three children ...
... four children ...
... 2 children...
... five children
END
@matches = $string =~ m/(?<=\s|^)[0-9]+|one|two|three|four|five|six|seven|eight|nine|ten(?=\s|$)/gi;
print join("\n", @matches);
产量
1
three
four
2
five