Parser: RegExp with named capture groups

This commit is contained in:
Olle Jonsson 2016-07-02 10:49:49 +02:00
parent d9e2cdeeac
commit b63d9d623e

View File

@ -275,8 +275,8 @@ module GitHubChangelogGenerator
# https://github.com/skywinder/ChangelogMerger # https://github.com/skywinder/ChangelogMerger
# ``` # ```
GIT_REMOTE_PATTERNS = [ GIT_REMOTE_PATTERNS = [
/.*(?:[:\/])((?:-|\w|\.)*)\/((?:-|\w|\.)*)(?:\.git).*/, /.*(?:[:\/])(?<user>(?:-|\w|\.)*)\/(?<project>(?:-|\w|\.)*)(?:\.git).*/,
/.*\/((?:-|\w|\.)*)\/((?:-|\w|\.)*).*/ /.*\/(?<user>(?:-|\w|\.)*)\/(?<project>(?:-|\w|\.)*).*/
] ]
# Returns GitHub username and project from git remote output # Returns GitHub username and project from git remote output
@ -288,15 +288,13 @@ module GitHubChangelogGenerator
user = nil user = nil
project = nil project = nil
GIT_REMOTE_PATTERNS.each do |git_remote_pattern| GIT_REMOTE_PATTERNS.each do |git_remote_pattern|
matches = Regexp.new(git_remote_pattern).match(git_remote_output) git_remote_pattern =~ git_remote_output
if matches && matches[1] && matches[2] if Regexp.last_match
puts "Detected user:#{matches[1]}, project:#{matches[2]}" user = Regexp.last_match(:user)
user = matches[1] project = Regexp.last_match(:project)
project = matches[2] break
end end
break if matches
end end
[user, project] [user, project]