我有这个例子:
This is a simple test text.
Yet another line.
START: This is the part that
needs match.
This part does not need
capture.
Wherever else text.
我想匹配这部分:
START: This is the part that
needs capture.
关键是我知道START:
在那里,它以一条新行结束,除了后面有空格。
我从:START: (.*?)
我和\ r \ n以及任何我想到的只有在没有空格的情况下才能匹配。
我不是一个小伙子,因为我很懒。我问了几个小时。
答案 0 :(得分:10)
这个怎么样:
preg_match(
'/^ # Start of line
START:\ # Match "START: "
.* # Match any characters except newline
\r?\n # Match newline
(?: # Try to match...
^ # from the start of the line:
\ + # - one or more spaces
.* # - any characters except newline
\r?\n # - newline
)* # Repeat as needed/mx',
$subject)
这假设所有行都是换行符。
答案 1 :(得分:1)
此代码可以与您的示例测试一起正常运行。
解决方法是在preg_match之前替换新行的标记(在!之后恢复!)和正则表达式结束时的Ungreedy修饰符(U)
<?php
$token = '#####';
$text = <<<TXT
This is a simple test text.
Yet another line.
START: This is the part that
needs match.
This part does not need
capture.
Wherever else text.
TXT;
$text = str_replace("\n", $token, $text);
if (preg_match('/(?P<match>START:(.)*)(' . $token . '){1}[^ ]+/Uu', $text, $matches))
{
$match = str_replace($token, "\n", $matches['match']);
var_dump($match);
}
$text = str_replace($token, "\n", $text);
输出将是:
string(42) "START: This is the part that
needs match."