简体   繁体   中英

Regex line matching with prefix

I'm current develop a GitHub hook to move some Zenhub issue placement when the commit message match certain string. What I'm trying to achieve is :

Progress #121, #128, #50

I want to extract the issue number here : 121, 128, 50 and the line must have a Progress prefix. Issue must be separated by comma, start with # and can have any number of prefix white space.

What I only achieved so far is this

^(?:Progress)(?:\s*#(\d+),?)+$

This only return to me the last capture group, which is 50 here.

I'd like to know where did I go wrong and how to achieve the desired outcome. Thank you.

EDIT : I wanted to used regex since I also wanted to solve these additional cases : "Progress" could start at middle of the string, or start of any line. If "Progress" start at the middle of the string, then I would have to check for any issue number after it.

So if there are any answer which solve this elegantly in Python ( other than regex ) is also welcome.

You're using a regex to match an entire pattern, and extract repeating sub-patterns from it at the same time. I'm not sure you can do both at the same time - it's either one or the other.

If you're using Python, I'd recommend making this easy. Use a simple str.startswith check and then extract issue numbers with regex.

if string.startswith('Progress'):
    return re.findall('#(\d+)', string)

Which should return ['121', '128', '50'] . You can adapt a similar methodolgy if you're working with JavaScript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM