[英]Multiline regex - how to match a sentence?
I need help extracting states from the following text:我需要帮助从以下文本中提取状态:
<br>
From: Florida
<br>
<br>
New York, Texas, California<br>
10% Delay chance
<br>
It's just part of a larger document, so there may be other occurrences of <br>
, thus I can't rely on their count.它只是一个较大文档的一部分,因此可能还有其他
<br>
出现,因此我不能依赖它们的计数。 However, there is only 1 occurrence of the following pattern: From:.+<br>.+<br>.+(.+).+% Delay chance<br>
.但是,以下模式仅出现 1 次:
From:.+<br>.+<br>.+(.+).+% Delay chance<br>
。 Using PHP:使用 PHP:
$pattern = "/.+From:.+<br>.+<br>.+(.+).+% Delay chance<br>/ism";
Since no one offered a solution except for (not really helpful) comments to avoid parsing HTML, here is what I came up with in the end:由于除了避免解析 HTML 的注释(不是很有帮助)之外,没有人提供解决方案,因此我最终想到的是:
$string = preg_replace ( "/\s+/" , " " , $string );
preg_match("!(?:.*?\K<br>){3}(?P<states>.*?)<br>!", $string, $match);
$match['states']
will contain the states. $match['states']
将包含状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.