dseslyh6662605 2015-08-11 16:28
浏览 53
已采纳

PHP preg_match两个值如何检查|| 操作者

I need to check if string contains F: # { blabla } or # F: { blabla } how to do that with preg_match? I try with this code but it does not run code when i add # before letter F:

if (preg_match('/F: ([A-za-z0-9]+) ([A-za-z0-9]+)( # (\{.*?\})){0,1}/', $line, $match) || preg_match('/# F: ([A-za-z0-9]+) ([A-za-z0-9]+)( # (\{.*?\})){0,1}/', $line, $match)) {
    echo 'STRING Contains F: and # F:';
}

Here is example:

<?php
$line = 'F: bbb bbb # { startdate=2015-08-10 | enddate=2015-11-10 | info=info | dealer=person548 | test=0 | emu=unknown | free=0 }';
/* $line = '# F: bbb bbb # { startdate=2015-08-10 | enddate=2015-11-10 | info=info | dealer=person548 | test=0 | emu=unknown | free=0 }'; */
if (preg_match('/(# )?F: ([A-za-z0-9]+) ([A-za-z0-9]+)( # (\{.*?\})){0,1}/', $line, $match)) {
    echo 'STRING Contains F: and # F:';
}
  • 写回答

1条回答 默认 最新

  • dongzhiyan5693 2015-08-11 18:17
    关注

    The pattern should look like this:

    $pattern = '/(F: #|# F:) \{ blabla \}/';
    

    Note the capturing ground (F: #|# F:). It matches one of the patterns separated by the |.

    Try it online

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

报告相同问题?

悬赏问题

  • ¥30 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页