【问题标题】:What to split my String using `preg_split`什么使用`preg_split`拆分我的字符串
【发布时间】:2017-10-08 17:04:57
【问题描述】:

这是我的原始字符串:-

$data = "<br />
Q.156)  Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: <br />
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department <br />

<br />
Q.157)  Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: <br />
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department <br />
<br />
Q.158)  Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: <br />
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department <br />
<br />
Q.159)  Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: <br />
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department <br />
<br />
Q.160)  Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: <br />
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department <br />
<br />
Q.161)  Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: <br />
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department <br />
<br />
Q.162)  Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: <br />
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department <br />
";

我想以这种格式将我的字符串拆分为数组

Array (
[0] => Q.156) Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: 
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department 

[1] => Q.157) Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: 
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department 

[2] => Q.158) Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: 
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department 

[3] => Q.159) Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: 
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department 

[4] => Q.160) Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: 
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department 

[5] => Q.161) Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: 
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department 

[6] => Q.162) Direction (156–160): Study the given pie-charts carefully to answer the questions that follow: 
Breaking up :- Number of Employees working in Different Departments of an Organisation, the Number of Males and the Number of Employees Who Recently Got Promoted in Each Department 
)

我知道我可以使用preg_split('/[Q[.]]+/', $data)

但我在 regex 方面有点弱。 请帮助我的正则表达式合并相应..

【问题讨论】:

  • 不,我必须保留中断标签。
  • @chris85 干得好....但是我需要像Q.1234) 这样的通用内容,因为您看到 . ("/d") .. 现在最终的正则表达式是什么

标签: php regex preg-match-all preg-split


【解决方案1】:

我认为preg_match_all 对你来说是一个更好的功能,因为你想匹配每一行。

preg_match_all('/Q\.\d+.*/', $data, $matches);
print_r($matches[0]);

演示:https://3v4l.org/pWs6c

您的正则表达式看起来有点过于松散,[Q[.]]+ 正在创建一个允许Q[. 的字符类。然后,] 会尝试匹配超过 1 次,因为 ] 是文字字符。您可以单独使用[.] 来匹配单个句点,或者\. 相同。

\d 是一个数字。
.* 允许零个或多个任意字符,不包括新行,这样可以捕获您的每个问题行。

如果您还需要尾随行,则可以使用此修改后的正则表达式。

/(Q\.\d+.*?)(?:(?:<br \/>|\n){3}|$)/

演示:https://regex101.com/r/Joo8wt/1/

此方法使用 s 修饰符,因此 . 扩展。

【讨论】:

  • 不,使用你的正则表达式我得到了我想要的输出。谢谢
  • 哦,太好了。我发布了一个更新以及考虑其他情况。
猜你喜欢
  • 1970-01-01
  • 2013-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-19
  • 1970-01-01
  • 1970-01-01
  • 2016-11-27
相关资源
最近更新 更多