标题: 如何用正则表达式判断输入的字符串为正整数
cabbac
新生入学
Rank: 1
注册用户



UID 130384
精华 0
积分 0(积分有什么用?)
帖子 20
阅读权限 10
注册 2005-8-17
状态 离线
发表于 2005-9-15 14:55  资料  个人空间  短消息  加为好友 
如何用正则表达式判断输入的字符串为正整数

<html>
<head>
    <title>Untitled</title>
</head>

<body>
<?php
$in="2344";
if(ereg("^(-{0,1}|\+{0,1})[0-9]+(\.{0,1}[0-9]+)[GV_contentText]quot;,$in))
    echo "Ok!";
else
    echo "Sorry,Please input again!";
?>

</body>
</html>

上面是一段例证程序,功能是用来判断输入的字符串是否为合法数字
菜鸟请大虾详解 ereg()部分 如何用该函数判断字符串是否为正整数?

顶部
不容错过的IBM 集群技术资源汇总
ServerOnly
自由撰稿人
Rank: 4
伊甸园杰出贡献会员


UID 86833
精华 12
积分 0(积分有什么用?)
帖子 2606
阅读权限 10
注册 2004-6-2
状态 离线
发表于 2005-9-15 16:05  资料  个人空间  短消息  加为好友  添加 ServerOnly 为MSN好友 通过MSN和 ServerOnly 交谈
我偷一点懒:

<html>
<head>
<title>Untitled</title>
</head>

<body>
<?php
$in="2344";
if(ereg("^(-{0,1}|\+{0,1})[0-9]+(\.{0,1}[0-9]+)[GV_contentText]quot;,$in)) {
        echo "Ok! It is a number, ";
        print ((strrpos($in,'-') or strrpos($in,'.'))?"but it is not + int!":"and it is a + int!";
} else echo "Sorry,Please input again!";
?>

</body>
</html>

当然这不是好办法





15 is too short
顶部
cabbac
新生入学
Rank: 1
注册用户



UID 130384
精华 0
积分 0(积分有什么用?)
帖子 20
阅读权限 10
注册 2005-8-17
状态 离线
发表于 2005-9-15 16:13  资料  个人空间  短消息  加为好友 
如何直接用正则表达式来判短呢?

用正则表达式限制只能输入数字:onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"

菜鸟我查到上面一段code 但不知如何加入页面中...

顶部
ServerOnly
自由撰稿人
Rank: 4
伊甸园杰出贡献会员


UID 86833
精华 12
积分 0(积分有什么用?)
帖子 2606
阅读权限 10
注册 2004-6-2
状态 离线
发表于 2005-9-15 16:22  资料  个人空间  短消息  加为好友  添加 ServerOnly 为MSN好友 通过MSN和 ServerOnly 交谈


$str="12345";
print preg_match("/^(0|[1-9][0-9]*)$/", $str)?"$str is int.":"$str is not int.";

以上php我没测试, 但perl我测试了:

#!/bin/perl
while (<>) {
        if ( $_ =~ /^([1-9][0-9]*)$/ ) {
                print "is int.\n";
                } else {
                print "is not int.\n";
        }
}






15 is too short
顶部
cabbac
新生入学
Rank: 1
注册用户



UID 130384
精华 0
积分 0(积分有什么用?)
帖子 20
阅读权限 10
注册 2005-8-17
状态 离线
发表于 2005-10-14 12:09  资料  个人空间  短消息  加为好友 
请教 版主达人一个菜鸟问题:

在perl 表达式中 $_ =~ /^([1-9][0-9]*)$/

应用到php 中 的正确写法是怎样的?如何表示“匹配”呢?

顶部
 



当前时区 GMT+8, 现在时间是 2008-5-17 11:39

    Powered by Discuz! 5.5.0  © 2001-2007 Comsenz Inc.
Processed in 0.056206 second(s), 5 queries

清除 Cookies - 联系我们 - Linux伊甸园 - Archiver - WAP