duanjia7607 2016-12-09 09:07
浏览 327
已采纳

正则表达式只从字符串末尾选择数字

I need a bit of a help with regex, fro example I have these strings:

  • "Dovolenka (8)"
  • "Bol raz jeden život 26/26"
  • "Parížski mohykáni č.1"
  • "Vtipnejší vyhráva 1985; Deň tlače,rozhlasu a televízi"

And I need to get the numbers "8", "26", "1", but not the "1985" that's a year not a part number. I got this so far '/(\d+).?$/' that works for picking the number but I do not know how to remove the string for preg_replace() function, so it only returns the wanted numbers.

Edit: What if i wanted only the strings, so vice versa ?

  • 写回答

1条回答 默认 最新

  • douqiaoru2583 2016-12-09 09:11
    关注

    Have a look at preg_match function, the third parameter is the array of matched items, as you enclose your numbers in brackets, you'll find them there. To be more precise, $matches[1] contains mathcing numbers, $matches[0] contains the whole matched expression.

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

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献