索引 0 处的预处理匹配


preg match at index 0

示例:

%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字符,以便我可以按段落提取。

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

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

演示

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

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

你可以试试这个。请参阅演示。

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