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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog