dongyi2534 2017-08-31 13:53
浏览 241
已采纳

正则表达式 - 在某个单词之后匹配4位数字作为完全匹配而不是组

I'm having hard time trying to figure out regular expression that will extract 4 digits long number after certain word as full match.

Here is the text:

FV 7017 FOR SOMETHING 1076,33 USD.

and here is my regular expression to extract the 4 digits number:

/FV (\d{4,})/

That will result in:

Full match  = `FV 7017`
Group 1 match = `7017`

Is it possible to exclude that "FV" word using regex to have that result as full match?

  • 写回答

2条回答 默认 最新

  • duanchuang3182 2017-08-31 14:04
    关注

    4 steps faster than a positive lookbehind, is to use \K to restart the fullstring match.

    /FV \K\d{4}/
    

    Pattern Demo

    PHP Implementation:

    $string='FV 7017 FOR SOMETHING 1076,33 USD.';
    echo preg_match('/FV \K\d{4}/',$string,$out)?$out[0]:'fail';
    // output: 7017
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥15 关于超局变量获取查询的问题
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata