Home > OS >  Unable to put a regular expression for a particular String
Unable to put a regular expression for a particular String

Time:12-09

In my Python code, I have a string that starts with Jira id like:-

<Jira Id in upper case>: <Commit Message>

for example, it appears like this:-

FD-0827: This is a test commit only

Here, 'FD' followed by a hyphen is important and remains static followed by dynamic numbers. I want the colon as a delimiter as well followed by any message.

So far I tried below code format for regex but have not been able to put some conditions around it:- JIRA_REGEX = "(\w )-(\d )" -> works but allows the lower case of FD as well and no check of colon JIRA_REGEX = "^[A-Z]-(\d )" -> does not work at all

In any case, it should look for the colon as well.

How can I put regex that strictly looks for

<Jira Id in upper case>: <Commit Message>

CodePudding user response:

This regex would also work: [A-Z]{2}-\d :(?:\s\w )

  • [A-Z]{2} - matches two upper case characters
  • - - matches the hyphen
  • \d - few digits of the Jira id
  • : - matches the colon
  • (?:\s\w ) - few space separated words

Demo

CodePudding user response:

Do you want sometthing like this ?

import re

JIRA_REGEX = "^[A-Z]{2}-\d :"

string = "FD-0827: This is a test commit only"

match = re.match(JIRA_REGEX, string)
if match:
    print("Valid Jira ID and commit message format")
else:
    print("Invalid Jira ID and commit message format")
  • Related