Search code examples
pythonregexregex-groupzapier

Python regex if present or else in Zapier workflow


I am working on creating a custom workflow using zapier. It requires to parse a given text using zapier formatter which uses Python Regex.

The python regex I am using is

(?P<action>My-DATA)\s+:desc:(?P<desc>.*):\s+:priority:(?P<priority>.*):\s+:label:(?P<lbl>.*):

Against an incoming text

My-DATA :desc:I would like to get details from https://mydata.org/this-is-my-data: :priority:3: :label:data:

There are couple of things which I am unable to figure out how I can achieve. Each section is delimited using : and section's name is also wrapped in :

Sometimes what happens is that the text will come in without the ending : for the section like this My-DATA :desc:I would like to get details from https://mydata.org/this-is-my-data :priority:3 :label:data

This text is missing : at the end of each section, but each new section always starts with : followed by the name of the section.

I am trying to figure out a regex which can still work as this one with or without the section ending :

Working regex example is at https://regex101.com/r/T2UJcP/2

Any help will be appreciated.


Solution

  • (?P<action>My-DATA)\s+:desc:(?P<desc>.*?):?\s+:priority:(?P<priority>.*?):?\s+:lbl:(?P<lbl>.*?):?\s*$