Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.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
Python正则表达式到PHP?_Php_Python_Regex - Fatal编程技术网

Python正则表达式到PHP?

Python正则表达式到PHP?,php,python,regex,Php,Python,Regex,我正在尝试将Python脚本转换为PHP 以下两个正则表达式在Python中工作: '/\*\*([\w\n\(\)\[\]\.\*\'\"\-#|,@{}_<>=:/ ]+?)\*/' '(?:\* ([\w\d\(\),\.\'\"\-\:#|/ ]+)|(?<= @)(\w+)(?: (.+))?)' 为什么?PCRE(包括preg\u match\u all)需要图案边界。您需要将整个模式包装在/、@、#、%或许多其他可能的选项中。我建议您使用%,因为您在这两种模式中

我正在尝试将Python脚本转换为PHP

以下两个正则表达式在Python中工作:

'/\*\*([\w\n\(\)\[\]\.\*\'\"\-#|,@{}_<>=:/ ]+?)\*/'
'(?:\* ([\w\d\(\),\.\'\"\-\:#|/ ]+)|(?<= @)(\w+)(?: (.+))?)'
为什么?

PCRE(包括
preg\u match\u all
)需要图案边界。您需要将整个模式包装在
/
@
#
%
或许多其他可能的选项中。我建议您使用
%
,因为您在这两种模式中似乎都没有使用它,即:


%(?:\*([\w\d\(\),\.\''“\-\:\ \ \ \ \ \ \ \ \ \/]+)(?原因是PHP需要在其正则表达式周围使用分隔符,因此它将第一个和第二个斜杠视为分隔符,并尝试将以下内容解析为修饰符

用新的分隔符环绕正则表达式并重试(我还删除了一些不必要的反斜杠):

'%/\*\*([\w\n()\[\].*\'”、@{}.=:/-]+?)\*/%

“%(?:\*([\w\d(),.\'”\:\ \ \/-]+)\(?必须使用正确的分隔符。第一个字符始终被视为分隔符。因此,PHP仅在
=:/
之前解释第一个表达式,然后找到不是有效修饰符的
]
。哈--明白了!我使用了“/”,但它实际上是在模式内部使用的!
Warning: preg_match_all() [function.preg-match-all]: Unknown modifier ']'
'%/\*\*([\w\n()\[\].*\'"#|,@{}_<>=:/ -]+?)\*/%'
'%(?:\* ([\w\d(),.\'"\:#|/ -]+)|(?<= @)(\w+)(?: (.+))?)%'