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 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用