使用RegEx从文件中提取文本

时间:2012-03-07 07:34:33

标签: php regex

我有一个包含以下数据的文本文件,

dup dx mul x0 add exch dup dy mul y0 add exch dr mul r0 add a1 a2 arcn dup dx mul x0 add exch dup dy mul y0 add exch dr mul r0 add a2 a1 arcn h dup dx mul x0 add exch dup dy mul y0 add exch dr mul r0 add a1 a2 arc dup dx mul x0 add exch dup dy mul y0 add exch dr mul r0 add a2 a1 arc h f %%EndResource The text i want to grap showpage

所以我想要%%EndResourceshowpage之间的所有文字。

任何帮助将不胜感激....

4 个答案:

答案 0 :(得分:1)

试试这个正则表达式

/%%EndResource(.*)showpage/g

要获取此值之间的值,请使用$1

答案 1 :(得分:1)

这应该有效

/(?<=%%EndResource).*?(?=showpage)/s

here on Regexr

(?<=%%EndResource)是断言的背后,它确保“%% EndResource”位于您想要获得的部分之前。

(?=showpage)是一个预见断言,它确保“%% EndResource”跟随您想要获得的部分。

.匹配任何字符(包括最后s修饰符的换行符)

*?匹配任意数量的字符和空字符串(!),但尽可能少(因为?

答案 2 :(得分:0)

此处不需要正则表达式。

$start = strpos($the_string, "%%EndResource") + count("%%EndResource");
$end = strpos($the_string, "showpage")

$result = substr($the_string, $start, $end - $start);

如果您想要松开空格,最后添加trim()

答案 3 :(得分:0)

以下正则表达式将捕获您在以后可以访问的组中所需的内容。

正则表达式是这样的:

%%EndResource(.*?)showpage

您可以使用this教程了解如何使用正则表达式组访问数据。