我想帮助用RegEx检测以下表达式
string“(x / x)”其中x是0-999之间的任何数字。 字符串“x of x”,其中x是0-999之间的任何数字。
通常,字符串是集合的标记,即10个中的4个,或者(3/5),其中第一个数字是项目,第二个数字是总数。
由于
答案 0 :(得分:1)
怎么样
“\ d {1,3} / \ d {1,3}”
和
“\ d {1,3} of \ d {1,3}”
答案 1 :(得分:1)
\([0-9]+\/[0-9]+\)
和
[0-9]+ of [0-9]+
答案 2 :(得分:1)
请参阅How to match numbers between X and Y with regexp?。
#!/usr/bin/perl
use strict;
use warnings;
my $num_re = qr/[0-9]|[1-9][0-9]|[1-9][0-9]{2}/;
for my $s qw( 1/10 100/500 a/456) {
if (my ($x, $y) = $s =~ m{^($num_re)/($num_re)$}) {
print "x is $x and y is $y\n";
} else {
print "$s does not match\n";
}
}
或只是
^([0-9]|[1-9][0-9]|[1-9][0-9]{2})\/([0-9]|[1-9][0-9]|[1-9][0-9]{2})$
如果您不介意违反DRY。