Regular Expressions 101

Community Patterns

Extract fields from repository url

1

Regular Expression
Python

r"
([a-z]+):\/\/([^/]*)\/([^/]*)\/(.*)\.git
"
gm

Description

It is used to extract fields from the repository URL Works in Python and JS

  • Group1 : protocol (Such as http or https)
  • Group2 : domain (Such as github.com or gitlab.com)
  • Group3: Name space (Such as your personal namespace or project namespace)
  • Group4: Project name (Such as your project name)

Python Example:

import re

#extract project and namespace from project url
def extract_fields_from_repo_url(repository_url):
  matches = re.search(r'([a-z]+):\/\/([^/]*)\/([^/]*)\/(.*)\.git', repository_url)
  protocol = matches.group(1)
  domain = matches.group(2)
  name_space = matches.group(3)
  project = matches.group(4)

  return {'protocol':protocol, 'domain':domain, 'name_space':name_space, 'project': project}

fields = extract_fields_from_repo_url('https://gitlab.com/gitlab-org/gitlab-ce.git')

print (fields)
Submitted by anonymous - 5 years ago