正则表达式:如果数字大于则匹配...? [英] Regular expressions: Matching if number is greater then...?

查看:141
本文介绍了正则表达式:如果数字大于则匹配...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个正则表达式来检查一个数值是否大于另一个值.就像我写 (if 10>8) ...

I am in the need of an regexp that checks if one numerical value is greater then another value. Pretty much like I would by writing (if 10>8) ...

很明显,我的代码片段不会让它发生,而且我缺乏正则表达式的经验来自己整理它.这可能与正则表达式有关吗?

Clearly, my snippet won't make it happen and I lack the experience of regexp to sort it out by myself. Is this possible to do with regular expressions?

^[1-9]+[0-9]+$

谢谢!

推荐答案

如果我正确理解您的问题,您需要一个正则表达式来匹配两个任意长度的连续数字,其中第二个大于第一个.这与 regexen 是不可能的.它显然不是一种常规语言,而且没有任何常见的扩展(反向引用、递归引用)强大到足以改变它.

If I understand your problem correctly, you want a regex which matches two consecutive numbers of arbitrarily length where the second is greater than the first. This is not possible to do with regexen. It's clearly not a regular language and none of the common extensions (backreferences, recursive references) are powerful enough to change that.

在某些正则表达式实现(例如 perl 的)中,可以将宿主语言中的代码嵌入到正则表达式中.在这种情况下,您当然可以只嵌入谓词 num1 <;num2 作为 perl 代码,但我不认为这算作正则表达式的解决方案.

In some regex implementations (e.g. perl's) it's possible to embed code in the host language into the regex. In that case you can of course just embed the predicate num1 < num2 as perl code, but I don't think that counts as a solution as a regex.

这篇关于正则表达式:如果数字大于则匹配...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆