简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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