Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/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
Php 如果不在某些单词前面,则匹配正则表达式_Php_Regex - Fatal编程技术网

Php 如果不在某些单词前面,则匹配正则表达式

Php 如果不在某些单词前面,则匹配正则表达式,php,regex,Php,Regex,更新:实际上php不支持长度可变的lookbehind。这种方法在php中是不可能的。任何可变长度查找都会出现错误“编译失败:查找断言不是固定长度” 我有以下正则表达式(我使用的是php): 与以下模式匹配: 22 years 49 ans 98 anos 如果输入前面有某些单词(“自”、“depuis”等),我需要使其不匹配 因此: 将匹配,而: Since 19 years Depuis 10 ans 将不匹配 我试过这个,但没有效果: /(?<!(depuis|since|mon

更新:实际上php不支持长度可变的lookbehind。这种方法在php中是不可能的。任何可变长度查找都会出现错误“编译失败:查找断言不是固定长度”

我有以下正则表达式(我使用的是php):

与以下模式匹配:

22 years
49 ans
98 anos
如果输入前面有某些单词(“自”、“depuis”等),我需要使其不匹配

因此:

将匹配,而:

Since 19 years
Depuis 10 ans
将不匹配

我试过这个,但没有效果:

/(?<!(depuis|since|monz))\d{2}\s\b(ans|year|years|sana|años|anos|sna)\b/i
/(?

提前感谢。

您的lookbehind格式不正确。lookbehinds中的“or”条件(当在PHP中的parens中使用时)需要相同的长度。否则,您可以像中一样逐个编写整个lookbehind条件

$str = "I'm 22 years and I have 49 years but Since 19 years and Depuis 10 ans";
preg_match_all(
'~
    (?<!
        \bdepuis\s  |
        \bsince\s   |
        \bmonz\s
    )
    \d{2}\s
    (?:
        ans?    |
        years?  |
        sana    |
        años?   |
        anos?   |
        sna
    )\b
~xi',$str,$m);
print_r($m);
$str=“我今年22岁,已经49岁了,但已经19岁了,10岁了”;
预赛(
'~

(?在文本中,
since
19
之间有一个空格,但在表达式中没有。您可能想添加它。您使用的是特定的编程语言吗?@Fernando,php-更新了问题使用此解决方案时有一个难题:连续空格字符应替换为1个空格字符。@nhahdh What d你是说?如果输入是
,因为uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuyears
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
代表空格),那么正则表达式将匹配19年,而它不应该匹配。据我所知,PHP正则表达式没有办法解决这个问题。除非OP在匹配中有可选的空格。然后
\s(如
\s*
)。您可以尝试想出一种方法来防止我指出的情况与之匹配。我找不到使用PHP正则表达式(外部空间或内部空间)的方法。如果您找不到方法,那么您应该用警告更新您的帖子。
/(?<!(depuis|since|monz))\d{2}\s\b(ans|year|years|sana|años|anos|sna)\b/i
$str = "I'm 22 years and I have 49 years but Since 19 years and Depuis 10 ans";
preg_match_all(
'~
    (?<!
        \bdepuis\s  |
        \bsince\s   |
        \bmonz\s
    )
    \d{2}\s
    (?:
        ans?    |
        years?  |
        sana    |
        años?   |
        anos?   |
        sna
    )\b
~xi',$str,$m);
print_r($m);
$pat =
'~
    (
        (?(?<=^)(?=\s*)             # if it is the beginning of the string
            (?:\s*)                 # match possible spaces
            |                       # otherwise match
            (?:
                (?<=\s)             # following a space,
                (?:                 # a word that is not listed below
                    (?!(?:
                        depuis  |
                        since   |
                        monz
                    ))
                    \S
                )+
                \s+                 # and 1 or more spaces
            )
        )
    )
    \d{2}\s+                        # then your pattern itself
    (?:
        ans?    |
        years?  |
        sana    |
        años?   |
        anos?   |
        sna
    )\b
~xi';
preg_match_all($pat,$str,$matches);
foreach ($matches[0] as $k => &$v)
    // replace the previous word if any
    $v = substr($v,strlen($matches[1][$k]));
// and delete the reference
unset($v);
print_r($matches);