preg匹配索引0

时间:2015-01-30 07:07:52

标签: php regex

示例:

%body
The family is a central socialization context from early childhood 
behavior \cite{dishi-sto2007,hengg-etal1998,johns-etal2005}.

Family management becomes more challenging for parents during the 
transition from middle childhood to adolescence. Typically developing 

说明:我需要从字符串"%body"中提取内容,直到第一段

我使用preg_match( '/%body\\n(.*?)\\n/', $text, $matches );进行提取。

输出 The family is a central socialization context from early childhood

由于\n字符在所有行中,我在字符串中得到一行。那么如何使用正则表达式找到索引0处的\n字符,以便我可以按段提取。

2 个答案:

答案 0 :(得分:0)

启用DOTALL修改器以在正则表达式中创建点以匹配偶数换行符。

preg_match( '~(?s)%body\n(.*?)(?:\n\n|$)~', $text, $matches );

DEMO

从组索引1中获取所需的字符串。

答案 1 :(得分:0)

%body\n([\s\S]*?)\n(?=\n|$)

你可以尝试一下。参见演示。

https://regex101.com/r/zM7yV5/16