Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.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,我需要匹配一个字符序列,但前提是它的前面没有“?”或“#”以及中间有0个或更多(任意)个通配符 $extension_regex = '/ (?<!\?|\#) # Negative look behind not "?" or "#" \/ # Match forward slash [^\/\?#]+ # Has one or more of any character except

我需要匹配一个字符序列,但前提是它的前面没有“?”或“#”以及中间有0个或更多(任意)个通配符

$extension_regex = 
'/
    (?<!\?|\#)    # Negative look behind not "?" or "#"
    \/            # Match forward slash
    [^\/\?#]+     # Has one or more of any character except forward slash, question mark and hash
    \.            # Dot
    ([^\/\?#]+)   # Has one or more of any character except forward slash, question mark and hash
/iux';

您不能在PCRE中的lookback中放置一个可变宽度断言,但您可能可以使用一个使用负lookahead的变通方法,类似这样的方法

^(?:(?![#?].*/index.php).)*(/index.php)
我添加了捕获组只是为了获得您想要匹配的部分,尽管它在这里可能没有实际用处

^(?(?![#?].*/index.php)。*
基本上可以匹配任何字符,只要前面没有紧跟着要匹配的字符串(
/index.php
)的

在C#中,您可能还可以使用:

(?<![#?].*)/index.php
(?这可能有助于:

$extension_regex = 'string';
$arr = array('?', '#', '0');//these are the forbidden characters
if(in_array(substr($extension_regex, 0, 1), $arr))
    echo "true";
else
    echo "false";

我对您的代码进行了调整,使其更加灵活。不仅仅是index.php。它现在可以工作:@cmcdragokai当然可以:)很高兴为您提供帮助!
$extension_regex = 'string';
$arr = array('?', '#', '0');//these are the forbidden characters
if(in_array(substr($extension_regex, 0, 1), $arr))
    echo "true";
else
    echo "false";