我正试图为php preg_split编写一个regexp,以在处理过程中捕获文本区域中某些类似"标题"的单词。
我想使用生成的数组来改进用户的格式,并在评论文章中创建一个精简的外观。
$returnValue = preg_split('/[^|'n]*['t| ]*'b(Pro|Contra|Conclusion)'b':['t| ]*/i',
$data['review_text'],
-1,
PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
这是我的示例文本输入
Intro line one, first part of the array
Pro:Pro:double Pro 1, no space between
Pro: Pro:double Pro 2, space between
Pro: test Pro:double Pro 3, characters between
Pro:
Pro:double Pro 4, linebreak betweem, should create an empty pro entry
Contra:
Conclusion: the last Contra was empty
Conclusion: this Contra: in this row should not match!
Conclusion: Test with spaces between Conclusion and :
Conclusion: this Conclusion was prefixed by a space
Conclusion: this Conclusion was prefixed by a Tab
Conclusion: this Conclusion was prefixed by two Tabs a space between
Conclusion : this Conclusion has a space between Conclusion and :
a final line with multiple line breaks in between, should be part of the last conclusion fragment
结果应包括[0]作为Intro行、4个Pro结果(带其分隔符)、1个Contra(空)和7个结论结果(带它们的分隔符)。唯一的Contra应该是空的,最后一行应该是最后一个结论的一部分
我正在尝试匹配类似的东西
- 行的开头,文件的开头
- 任何空白字符出现零次或n次
- Pro、Contra或结论的任何版本(忽略大小写)
- 任何空白字符出现零次或n次
- :
按此顺序
首先,[^|'n]*
表示0个或多个不是管道|
或换行符的字符['t| ]*
表示0个或多个不是制表或管道|
或空格的字符。
我想你想要:
/'s*'b(Pro|Contra|Conclusion):['t ]*/i
在@M42的帮助下,我找到了正确的方法。。。
'/'n['t ]*'b(Pro|Contra|Conclusion)['t ]*:['t ]*/i'
由于只缺少"开始文件而不是新行",这几乎正是我想要的(尽管仍在测试以制作shure)。现在,我在字符串之前添加了一个"''r''n",稍后当我修剪()字符串片段时,这个字符串会被剥离。
完整的PHP调用看起来像这个
$returnValue = preg_split('/'n['t ]*'b(Pro|Contra|Conclusion)['t ]*:['t ]*/i', $data['review_text'], -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
为了防止你想知道,为什么我在回复M42时使用Fazit而不是结论,我正在为一个德国网络应用程序编写代码,所以我必须翻译每一个副本&粘贴到StackOverflow。(ಠ_ಠ)