dongqishou7471 2011-05-02 22:14
浏览 48
已采纳

php Regex返回String中的一些整数

I need help with regex, please.

I thought I had svn happily integrated with Mantis until I hit a problem with a checkin containing multiple issues. I'm using Mantisbt 1.2.5

The commit message I'm trying to support could like: "Issues #74 78 112 Did something to line 485 that only took 3 hours and 27 minutes". I need my regexp to return [74, 78, 112] (but not [485, 7, 27]).

My 'current' $g_source_control_regexp = '/\b(bug|issue)[s]{0,1}\s*[#]{0,1}\s*(\d+\s+)+/i' seems to be returning 1 element of 'Issues #74 78 112' which updates nothing

Any advice appreciated.

Jim

  • 写回答

1条回答 默认 最新

  • douguai4653 2011-05-02 22:49
    关注

    I don't think you can do that with just regex. Repeated capture groups capture just the last iteration (read more about Repeating a Capturing Group vs. Capturing a Repeated Group)

    Otherwise I'd do it like this (note that I'm not a PHP programmer...):

    str.match(/\b(?:bug|issue)s?\s*#?\s*((?:\d+\s+)+)/i)[1].trim().split(/\s+/)
    result: ["74", "78", "112"]
    
    • (?:) are non-capturing groups
    • matches[0] is usually the full pattern match
    • matches[1] is the first captured group (the only one in this case)
    • trim() is needed to get rid of an extra space at the end (without it you would get an empty group at the end ["74", "78", "112",""])
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题