dpxpz37157 2014-08-26 18:13
浏览 124
已采纳

PHP正则表达式只获得两个字符串之间的第一个值

I have a string that looks like this

aa bcdef123efg hh aa lklmasd456 hh (whitespace and bolding is used for illustration)

If I have 456, I want to find where 456 is and grab everything in between aa and hh.

This is the regex that I was able to come up with so far (?=aa)(.*)456(.*)(?=hh)

However, that doesn't stop at the first occurance of aa it encounters (backwards from the 456 match)

What would be the proper syntax for what I'm trying to achieve.

  • 写回答

1条回答 默认 最新

  • dqo88037 2014-08-26 18:19
    关注

    You can use this lookaround based regex:

    aa((?:(?!hh).)*?456(?:(?!hh).)*?)hh
    

    RegEx Demo

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

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数