Search code examples
phpregexsvnmantis

php Regex to return some of the Integers in a 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


Solution

  • 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",""])