duanchao4445 2012-03-21 09:58
浏览 268
已采纳

正则表达式:匹配所有直到某个单词(PHP)

I'm processing a file with PHP.

This file contains a few blocks, which always start with the word "Step" (step 1, step 2 etc), and always end with "end step". Withing, it can have newlines, but never 2 consequently.

I'm trying to build a regex that will turn this into an array.

What I have so far is

preg_match_all("/Step([^\"end step\"]*)/s", $content, $matches);

The /s at the end of the patnern is to allow newslines to be included too. But of course, this does not work since all letters from "end step" are excluded, not only if they form 1 word. How can I write the correct regex?

  • 写回答

1条回答 默认 最新

  • dongshiran7000 2012-03-21 10:02
    关注

    One simple way:

    preg_match_all('/Step(.*?)"end step"/s', $content, $matches);
    

    This matches any text from Step to the nearest "end step". But it needs to backtrack after every single character which could be slow.

    Slightly more explicit and possibly more efficient::

    preg_match_all('/Step((?:(?!"end step").)*)/s', $content, $matches);
    

    This matches all the text from Step up to but not including the nearest "end step". It will match until the end of the string if "end step" never comes. This regex looks ahead at every step to check whether the string "end step" could be matched there and ends the match if that's true.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳