doushangan3690 2014-05-30 22:39
浏览 20
已采纳

如何使用一个正则表达式而不是两个匹配值?

I want to catch values from this string:

$data='1=A
5=B
7=F
11=G';

The next piece of code works fine, but is using two regular expressions. Is there a way to use only one?

$i = 1;

if (!preg_match("/
$i=(.*)/",$data,$m))
{
  preg_match("/^$i=(.*)/",$data,$m);
}
  • 写回答

1条回答 默认 最新

  • doucezhu3570 2014-05-30 22:54
    关注

    I think multiline mode is what you're looking for:

    preg_match("/^$i=(.*)/m",$data,$m);
    

    The m modifier allows the start anchor (^) to match the beginning of any line instead of just the beginning of the string. It still prevents a match on (11=G) because the 1 has to be at the beginning of the line and immediately by =. Is that what you meant?

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度