# 正则表达式匹配字符串

Home / 正则表达式匹配字符串

### 正则表达式匹配字符串

2015-2-3 00:40:52 | RegularExpression | 没有评论

```#!/usr/bin/perl -w

if (\$ARGV[0] =~ /^\d+\$/){
print "Numbers!\n";
} else {
print "Not Numbers!\n";
}

lihui@2015 ~
\$ ./hi.pl 33131
Numbers!

lihui@2015 ~
\$ ./hi.pl 33131as32
Not Numbers!```

```#!/usr/bin/perl -w

if (\$ARGV[0] =~ /^[+-]?\d+(\.\d+)?\$/){
print "Numbers!\n";
} else {
print "Not Numbers!\n";
}

lihui@2015 ~
\$ ./hi.pl -1
Numbers!

lihui@2015 ~
\$ ./hi.pl -1.0
Numbers!

lihui@2015 ~
\$ ./hi.pl +1.0a
Not Numbers!

lihui@2015 ~
\$ ./hi.pl 1.07
Numbers!```

```#!/usr/bin/perl -w

if (\$ARGV[0] =~ /^[+-]?(\d+)(\.\d+)?\$/){
print "\\$1 = \$1\n";
print "\\$2 = \$2\n";
print "Number is \$1\$2"
} else {
print "Not Numbers!\n";
}

lihui@2015 ~
\$ ./hi.pl 1.07
\$1 = 1
\$2 = .07
Number is 1.07```

```lihui@2015 ~
\$ ./hi.pl -3
\$1 = 3
Use of uninitialized value \$2 in concatenation (.) or string at ./hi.pl line 5.
\$2 =
Use of uninitialized value \$2 in concatenation (.) or string at ./hi.pl line 6.
Number is 3```

```#!/usr/bin/perl -w

if (\$ARGV[0] =~ /^[+-]?(\d+)(\.\d+)?\$/){
print "\\$1 = \$1\n" if \$1;
print "\\$2 = \$2\n" if \$2;
if (\$2) {
print "Number is \$1\$2";
} else {
print "Number is \$1";
}
} else {
print "Not Numbers!\n";
}

lihui@2015 ~
\$ ./hi.pl -3
\$1 = 3
Number is 3```