使用正则表达式匹配字符串中的特定行

sta*_*een 1 php regex

我有一个字符串,其中包含一些由新行分隔的子字符串.

例如 :

$x="This is the first line.
      This is the second line.
      This is the third line.";
Run Code Online (Sandbox Code Playgroud)

我想从这个字符串中获取第三行,

到目前为止我的正则表达式:

/\s*([^\n]+)/i
Run Code Online (Sandbox Code Playgroud)

但这会返回整个字符串,

我也试过了

/\n{3}\s*([^\n]+)/i
Run Code Online (Sandbox Code Playgroud)

它没有任何匹配.

正则表达式有什么方法可以解决我的问题吗?在过去的30分钟里,我一直在努力解决这个问题,但我的所有尝试都失败了.

 preg_match_all("/\s*([^\n]+)/i",$x,$m);
print_r($m);
Run Code Online (Sandbox Code Playgroud)

谢谢!

Two*_*aws 5

除非我误解你的情况的复杂性,否则你应该能够explode()像这样使用:

$split = explode("\n", $x);
print_r($split[2]);
Run Code Online (Sandbox Code Playgroud)