-1

I have the following patterns:

"[list of tickets] [] : List of tickets" Example 1

How can I parse the above strings to validate the pattern below.

[Jira-Ticket-list] [TAG] : <Some notes>"

Also how can I parse the tickets and tag out of the string above?

Expected output:

Example 1:

Tickets = [list of tickets]

Tag = [TAG-0.3.4]

coderWorld
  • 508
  • 4
  • 19

1 Answers1

1

You can use re.search to check for the pattern r'(\[(?:JIRA\-\d+,?)*\])\s+(\[TAG-[\d.]+\])\s+:\s+(.*)$' and if it matches, <match>.groups() would contain the tickets, tag and description

If the regexp pattern is too strict for you, you can use the simple pattern r'(\[.*?\]) (\[.*?\]) : (.*)$' instead

>>> import re
>>> 
>>> s = "[JIRA-1115,JIRA-1917] [TAG-0.3.4] : List of tickets"
>>> tickets, tag, descr = re.search(r'(\[(?:JIRA\-\d+,?)*\])\s+(\[TAG-[\d.]+\])\s+:\s+(.*)$', s).groups()
>>> tickets, tag, descr
('[JIRA-1115,JIRA-1917]', '[TAG-0.3.4]', 'List of tickets')
>>> 
>>> s = "[JIRA-1116] [TAG-0.3.5] : Only 1 ticket now"
>>> tickets, tag, descr = re.search(r'(\[(?:JIRA\-\d+,?)*\])\s+(\[TAG-[\d.]+\])\s+:\s+(.*)$', s).groups()
>>> tickets, tag, descr
('[JIRA-1116]', '[TAG-0.3.5]', 'Only 1 ticket now')
Prem Anand
  • 2,189
  • 12
  • 16
  • What if instead of the above example I had "[JIRA-1115,JIRA-1917] -[0.3.4] : Some description" In that case without a version and with the '-' inbetween how could I update it? – coderWorld Jul 27 '20 at 20:02
  • The generic pattern `r'(\[.*?\]) -(\[.*?\]) : (.*)$'` would match – Prem Anand Jul 27 '20 at 20:08
  • Yes but the '-' can have white spaces before and after. How can I ignore the whitespaces before and after the '-'. For example: "[JIRA-1115,JIRA-1917] - [0.3.4] : – coderWorld Jul 27 '20 at 20:13
  • Use `\s*` to match the whitespaces.. So `r'(\[.*?\])\s*-\s*(\[.*?\]) : (.*)$'` should work – Prem Anand Jul 27 '20 at 20:53