【发布时间】:2012-07-04 12:32:09
【问题描述】:
我正在尝试在 preg_match_all 中的 PHP 中使用这个 reg exp
/\d+ (?:<[^>]+>)(?:<[^>]+>)(\S+.*\S+)(?:<[^>]+>)\s*(\S+) (?:L|R)\s*\w* \w*\s*(?:\w+\s*){14}(\d+)\s*(\d)\s*(\d*\xA0*\d{3}\xA0*\d{3})/is
有一些数据样本:
38 <A NAME="Philip McRae"><A HREF="xtrastats.html#Philip McRae">Philip McRae</A> C L OK 58 71 69 49 33 89 71 45 48 69 50 35 32 61 21 3 787 000
43 <A NAME="Alexander Nikulin"><A HREF="xtrastats.html#Alexander Nikulin">Alexander Nikulin</A> C L OK 41 68 71 40 28 90 67 29 31 60 31 37 34 50 26 0 0 000 <a href="http://www.hockeydb.com/ihdb/stats/pdisplay.php?pid=78680" target="_blank">HDB</a>
20 <A NAME="Christian Hanson"><A HREF="xtrastats.html#Christian Hanson">Christian Hanson</A> C R OK 57 72 71 54 33 79 70 42 45 71 46 40 36 60 25 1 875 000 <a href="http://www.hockeydb.com/ihdb/stats/pdisplay.php?pid=73824" target="_blank">HDB</a>
我得到了大约 1500 行。
我需要匹配这个:
Philip McRae, C, 21, 3, 787 000 (Name, Position, Age, Contract Lenght, Salary)
每次我运行我的代码时,我都会收到一个致命错误:Maximum execution time of 30 seconds exceeded 错误。
经过一番搜索,我在脚本顶部添加了这一行,但这并不能解决我的问题
ini_set("pcre.backtrack_limit",10000000);
任何人都可以帮助我使用这个 reg exp 进行一些优化吗?
问候。
帕特里克
【问题讨论】:
-
我们需要知道你想让表达式做什么来帮助你。
-
在我的问题中添加了数据样本。
-
我在数据样本中没有得到与该正则表达式的任何匹配。您是否使用较少的数据进行了测试?
-
@jared 你在 Windows 或 Linux 上测试它吗?在 Windows 上,将\xA0 替换为 .(dot)
-
使用正则表达式来匹配这种复杂的东西对我来说总体来说很糟糕。为什么不把它分成几个更小的正则表达式并使用状态机呢?没有构建正则表达式来解析 HTML 的标签结构。还有,用 DOMDocument 来解析呢?
标签: php regex optimization pcre backtracking