简体   繁体   English

PHP正则表达式空间或换行符之前或之后

[英]PHP regular expression space or newline before or after

I need to detect locations of start and end in several lines of pseudo code. 我需要在几行伪代码中检测开始和结束的位置。

Basically, if the word "start" or the word "end" has a space or newline before and after, I need to record the match. 基本上,如果单词“开始”或单词“结束”前后都有空格或换行符,则需要记录匹配项。

The space/newline recognition is important since start or end could be part of the contents that appears between start and end, ie 空格/换行符识别很重要,因为开始或结束可能是出现在开始和结束之间的内容的一部分,即

start
 start_time = 2
  end_time = 5
end

So far tried 到目前为止尝试过

preg_match('/((\s)|( *)(start)(\s)|( *))|((\s)|( *)(end)(\s)|( *))/i',
$line, $matches, PREG_OFFSET_CAPTURE);

...and variations. ...和变化。

You can use this regex to capture this block: 您可以使用此正则表达式捕获此块:

(?<= |\A)start(?=\s)[\s\S]*(?<=\s)end(?=\s|\z)

RegEx Demo 正则演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM