简体   繁体   中英

Ruby regex: parsing git commit messages for youtrack issue codes

I'd like to parse and remove youtrack issue codes embedded anywhere in commit messages.

For those who have never used youtrack, you can specify issue codes in commits as follows:

#<project>-<issue#> <commit msg> 
e.g. #PROJ-3 I like to use git and youtrack

or...

e.g. I'm silly #PROJ-3 and like to use git and youtrack

I have the following regex...

$remove_issue_regex = /( |^)#(\w+-\d+):? ? -? ?/

...which I feed into a sub method replacing the substring with an empty string. But it's ugly and might not work if the user formats their messages in a silly way. Does anyone know a more elegant way to do this?

Use a lookbehind,

(?<= |^)#(\w+-\d+)(?=: - )?

Rubular

OR

(?<= |^)#(\w+-\d+)(?:: - )?

Rubular

Just replace the whole string with the first captured group to get only the <project>-<issue> format.

$remove_issue_regex = /#\\w+-\\d+ (.*)/

将提交消息放在图章之后作为第一个捕获组

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