I have the following strings, what I would like to get are the User name (user) and the project name (project-1) for example.
But I need two different regular expressions.
- One to get the user's name only
- One to get only the name of the project
Can you give me some advice?
https://github.com/user/project-1.git
https://github.com/user/project-1
[email protected]:user/project-1.git
CodePudding user response:
User: s/^.*github\.com[:\/]([^\/] )\/.*$/\1/
(https://regex101.com/r/EHSIvr/1)
Project: s/^.*\/([^\/] ?)(?:\.git)?$/\1/
(https://regex101.com/r/tzOFDm/1)
The links to regex101.com (which I recommend to use) also explain step by step both regex.
In particular, notice the use of quantifier ?
(lazy: match as few characters as possible) in the second expression, to avoid including .git
.
CodePudding user response:
user: r'(?:. [:/])(. ?)(?:/)'
explanation > https://regex101.com/r/Vh4Z9w/1
project: r'(?:. /)(. ?)$'
explanation > https://regex101.com/r/TJ7H2v/1
where:
(?: ) - non-capturing group
. - any single character
- one or more
$ - end of string
? - non greedy, as less as possible