0

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

2
  • 4
    [#]{0,1} can simply be written #? Commented May 2, 2011 at 22:17
  • Your regex seems to work fine then. And you'll just need a second one to split up the eventual list. Commented May 2, 2011 at 22:23

1 Answer 1

1

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",""])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.