Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/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,所以我正在做一个项目,为一个交易卡游戏建立一个价格指南。请原谅这里的无聊程度。我正在从一个网站获取数据 $data = mb_convert_encoding(file_get_contents("http://yugioh.wikia.com/api.php?action=query&prop=revisions&titles=Elemental%20HERO%20Shining%20Flare%20Wingman&rvprop=content&format=p

所以我正在做一个项目,为一个交易卡游戏建立一个价格指南。请原谅这里的无聊程度。我正在从一个网站获取数据

$data = mb_convert_encoding(file_get_contents("http://yugioh.wikia.com/api.php?action=query&prop=revisions&titles=Elemental%20HERO%20Shining%20Flare%20Wingman&rvprop=content&format=php"), "HTML-ENTITIES", "UTF-8");
然后我使用一系列正则表达式语句对其进行解析

preg_match_all('/(?<=\|\slore)\s+\=(.*)/', $data, $matches);
$text = $matches[1][0]; //it prints out here just fine
$text = preg_replace("/(\[\[(\w+|\s)*\|)/sx", "" , $text); //it disappears if I try to print it here
$text = preg_replace("/\[\[/", "" , $text);
$text = preg_replace("/\]\]/", "" , $text);
这个正则表达式包含嵌套的量词:
\w
+
量词一起使用,
*
应用于整个替换组。这会创建大量回溯步骤,并导致错误

避免此问题的最佳方法是通过字符类
[\w\s]*
(匹配0个或更多字母数字字符或空白符号)

见:


还请注意,您不需要
x
修饰符(因为模式本身没有注释和无意义的空格)和
s
修饰符(因为模式中没有

请在第一次正则表达式通过后提供
文本
内容(即
$matches[1][0]
内容)@shA.t
s
告诉。要包含换行符,
x
包含扩展的注释和空格。@Stribizev我添加了文本公平竞争-是的,我以前尝试过,你是对的-这确实是谚语中的一个痛苦!灾难性回溯。你需要使用
$text=preg\u replace(/(\[\[([\w\s]*))\\\\\\\)/“,”,$text);
。检查。@Stribizev这是正确的解决方案,如果您将其作为答案发布,我将接受。谢谢大家的帮助!
 "[[Elemental HERO Flame Wingman]]" + "[[Elemental HERO Sparkman]]"
Must be [[Fusion Summon]]ed and cannot be [[Special Summon]]ed by other ways. This card gains 300 [[ATK]] for each "[[Elemental HERO]]" card in your [[Graveyard]]. When this card [[destroy]]s a [[Monster Card|monster]] [[Destroyed by Battle|by battle]] and [[send]]s it to the Graveyard: Inflict [[Effect Damage|damage]] to your opponent equal to the ATK of the destroyed monster in the Graveyard.
$s = "\"[[Elemental HERO Flame Wingman]]\" + \"[[Elemental HERO Sparkman]]\"\nMust be [[Fusion Summon]]ed and cannot be [[Special Summon]]ed by other ways. This card gains 300 [[ATK]] for each \"[[Elemental HERO]]\" card in your [[Graveyard]]. When this card [[destroy]]s a [[Monster Card|monster]] [[Destroyed by Battle|by battle]] and [[send]]s it to the Graveyard: Inflict [[Effect Damage|damage]] to your opponent equal to the ATK of the destroyed monster in the Graveyard.";
$s = preg_replace('/(\[\[([\w\s]*)\|)/', "" , $s);
echo $s;