Home > front end >  Python Regex - How do I fetch a word after a specific word in a string using python regex?
Python Regex - How do I fetch a word after a specific word in a string using python regex?

Time:10-06

I need to fetch "repo-name" which is "sonar-repo" from the above multi-line commit string. Can this be achieved with regex? Output Expected: sonar-repo

Here is the string which I need to read using regex,

commit_message=
"""repo-name=sonar-repo;repo-title=Sonar;repo-description=A little demo;repo-requester=Jack
"""

CodePudding user response:

You should be able to use regex to look for repo-name= and then look for the ; right after and get what's inbetween. Something like this:

(?<=repo-name=).*?(?=;)

Tested it here with regex101

CodePudding user response:

Try this:

import re

commit_message= 'repo-name=sonar-repo;repo-title=Sonar;repo-description=A little demo;repo-requester=Jack'

print(re.search(r'repo-name=(.*?);', commit_message).group(1))

Output:

sonar-repo
  • Related