dongnue4923 2013-10-03 15:41
浏览 31

PHP正则表达式用于匹配两个句子中的数字

I'm writing a php script where I need to create a regex rule to match two types of strings ( string1 and string2 in below code) and the matched number needs to be in $matches[1] ( should not be matches[2] ).

<?php
$ticketNumber1 = $ticketNumber2 = '';
$string1 = "[Ticket ID: 309972] New Support Ticket Opened";
$string2 = "Ticket #: 656398";
$regex = "/Ticket #|ID: (\d+)/";
if(preg_match($regex, $string1, $matches))
{
        $ticketNumber1 = $matches[1];
}
if(preg_match($regex, $string2, $matches))
{
        $ticketNumber2 = $matches[1];
}
echo "TN1: $ticketNumber1
";
echo "TN2: $ticketNumber2";
echo "
";
?>

Can any one help me with this? What should be the regex to be used to get the result?

Kindly help.

  • 写回答

1条回答 默认 最新

  • dqf60304 2013-10-03 15:44
    关注

    You need to limit the scope of the operator OR by using a non-capturing group:

    /Ticket (?:#|ID): (\d+)/
    

    regex101 demo

    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)