Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 正则表达式匹配3个数字_Regex_Digits - Fatal编程技术网

Regex 正则表达式匹配3个数字

Regex 正则表达式匹配3个数字,regex,digits,Regex,Digits,目前我正在尝试创建一个正则表达式,在某些特定情况下可以匹配3个数字。我现在已经尝试了各种尝试,但它不能用一个单一的表达式-它要么是“假阳性”,要么是“匹配错误的数字” 大写:我想匹配 出现在字符串的开头 出现在绳子里面的某个地方 (字符串结尾不可能) 如果: 没有其他3位数字组与此条件匹配。(模棱两可) 该组后面没有“p”或“i” 该小组不是由“x”领导的 在示例中(我想匹配()中的数字): 这是(321)个例子 (321)也 包括(321)//基本上不可能,但不会受伤 这(321)有

目前我正在尝试创建一个正则表达式,在某些特定情况下可以匹配3个数字。我现在已经尝试了各种尝试,但它不能用一个单一的表达式-它要么是“假阳性”,要么是“匹配错误的数字”

大写:我想匹配

  • 出现在字符串的开头
  • 出现在绳子里面的某个地方
  • (字符串结尾不可能)
如果:

  • 没有其他3位数字组与此条件匹配。(模棱两可)
  • 该组后面没有“p”或“i”
  • 该小组不是由“x”领导的
在示例中(我想匹配
()
中的数字):

  • 这是(321)个例子
  • (321)也
  • 包括(321)//基本上不可能,但不会受伤
  • 这(321)有另一个p:122p的群
  • 该(321)具有具有I:123i的另一个组
  • 这个x235应该被忽略,因为(123)是我想要匹配的
  • (123)是我想要的,而不是x111或125p或999i
  • 在这种情况下,没有解决方案555
(我需要像(1个数字)(2个数字)-但这只是对3个数字匹配的一点修改)

我上次的尝试是这样的:

(?:[^x]|^)(\d{1})(\d{2})[^pi]
re_n = (?:[^x]|^)\d\d\d(?:[^ip]|$)

然而,它在最后一个案例中失败了。我试图用
preg_match_all(…)==1来覆盖这一点,以确保只匹配一个结果

但是,现在像“101 202”这样的测试字符串将是正数,因为第一个检查匹配
101
(包括空格),然后在
202
上不匹配,这使得模式假设
101
是唯一有效的解决方案-这是错误的

(?:[^x]|^)(\d{1})(\d{2})[^pi]

有什么想法吗


注意:它应该在不同的正则表达式引擎中工作,无论是php、javascript、java、.net还是Ook!:)

我不确定你是否想要这个,试试看:

JAVASCRIPT

var myregexp = /(?:\b[\s]?|[^x])([\d]{1}[\d]{2})(?:[^pi]|[\s]?\b)/m;

PHP

JAVA

红宝石

PYTHON

C(PCRE)


我们可以这样写下您想要的号码:

(?:[^x]|^)(\d{1})(\d{2})[^pi]
re_n = (?:[^x]|^)\d\d\d(?:[^ip]|$)
那么整个表达就是:

^(?!.*re_n.*re_n.*$).*(re_n)
这基本上消除了在行开始锚点之后使用负前瞻的双精度数字,然后匹配一个有效的数字

插值表达式看起来很难看:

/^(?!.*(?:(?:[^x]|^)\d\d\d(?:[^ip]|$)).*(?:(?:[^x]|^)\d\d\d(?:[^ip]|$)).*$).*((?:(?:[^x]|^)\d\d\d(?:[^ip]|$)))/
此Perl代码:

my $re_n = qr/(?:[^x]|^)\d\d\d(?:[^ip]|$)/;
while (<DATA>) { chomp;
    if (/^(?!.*$re_n.*$re_n.*$).*($re_n)/) {
        print "$_: $1\n";
    } else {
        print "$_: NONE\n";
    }   
}

__DATA__
This is 321 an example.
321 also
including 321 //basically not possible, but can't hurt.
this 321 has another group with a p: 122p
this 321 has another group with a I: 123i
this x235 should be ignored cause 123 is what i want to match.
123 is what i want, not x111 or 125p or 999i
in this 111 case there is no solution 555

我觉得你把事情复杂化了,你只是想找个地方看看。尝试
~(?并告诉我它得到的匹配有什么问题。@HamZa也考虑了环顾四周。我的方法是
(?)-但是,在Debuggex上,lookback失败,出现编译错误,其他Regex测试程序也不返回任何匹配项,所以我想使用更“可靠”的方法比lookbehinds更有效的解决方案:您会遇到一个错误,因为JavaScript不支持lookbehinds。每个现代正则表达式引擎(java、.net、python、pcre等)都支持lookaround。它是一个强大的工具。所以,如果您使用的是php,为什么不使用它呢?@HamZa问题是,我不想提供“任何”客户端是一个用于验证的模式。我不知道客户端是否使用Javascript、php、.net或其他什么。声明“Javascript未移植”将是最后一种方法:)这是你的问题。有一些黑客方法可以“模拟”lookbehind,但这完全取决于语言。在Javascript中,你可以使用回调。因此声明在这种情况下,“我想要一个通用正则表达式”是疯狂的,因为正则表达式的语法不是通用的,更不用说它们的功能了。看看什么不是
PCRE
,我开玩笑说…我从来没有想过要提供几乎所有可能的语言变体。我将开始为
regex
问题做这件事。@MattGreen:是的,:)@MattGreen PCRE:)虽然,我不确定OP是否想要这样做。@Tuga:+1对于您在那里所做的工作:)但这并不完全是我需要的:我正在创建一个API,用户可以通过编程方式发布数据。为了方便起见,我不想在发布每个字段之前向用户提供一个正则表达式以及字段定义来验证它的数据。当客户端检索此信息时,我不知道它是php还是javascript客户端。始终提供所有正则表达式似乎不合适。你说得对,这很难看,但它似乎在不同的正则表达式实现中运行良好。关于整个表达式,这是一个好主意。
^(?!.*re_n.*re_n.*$).*(re_n)
/^(?!.*(?:(?:[^x]|^)\d\d\d(?:[^ip]|$)).*(?:(?:[^x]|^)\d\d\d(?:[^ip]|$)).*$).*((?:(?:[^x]|^)\d\d\d(?:[^ip]|$)))/
my $re_n = qr/(?:[^x]|^)\d\d\d(?:[^ip]|$)/;
while (<DATA>) { chomp;
    if (/^(?!.*$re_n.*$re_n.*$).*($re_n)/) {
        print "$_: $1\n";
    } else {
        print "$_: NONE\n";
    }   
}

__DATA__
This is 321 an example.
321 also
including 321 //basically not possible, but can't hurt.
this 321 has another group with a p: 122p
this 321 has another group with a I: 123i
this x235 should be ignored cause 123 is what i want to match.
123 is what i want, not x111 or 125p or 999i
in this 111 case there is no solution 555
This is 321 an example.:  321 
321 also: 321 
including 321 //basically not possible, but can't hurt.:  321 
this 321 has another group with a p: 122p:  321 
this 321 has another group with a I: 123i:  321 
this x235 should be ignored cause 123 is what i want to match.:  123 
123 is what i want, not x111 or 125p or 999i: 123 
in this 111 case there is no solution 555: NONE