1

With Python I need to parse the following string, that can be written in four different ways:

:param MyParam: My description [MyValue] {MyGroup}  
:param MyParam: My description [MyValue]  
:param MyParam: My description {MyGroup}  
:param MyParam: My description  

The expectation is to extract MyParam, My description, MyValue and MyGroup

I tried with the following pattern:

^\:param(?P<param>.*)\:(?P<desc>.*)\s*(\[(?P<value>.*)\])?\s*(\{(?P<group>.*)\})?$

but in all cases it is too greedy.

Using the following one:

^\:param(?P<param>.*)\:(?P<desc>.*) (\[(?P<value>.*)\])? (\{(?P<group>.*)\})?$

I get valid results only if there are spaces according to the pattern expectations.
Any idea how to fix this ?

2
  • Use the non-greedy variants, i.e. .*? instead of .*. Commented Apr 22, 2021 at 14:43
  • BTW, you don’t have to escape the colons. Commented Apr 22, 2021 at 14:45

1 Answer 1

3

You may use this regex to parse all the possible combination mentioned in your question:

^:param\s+(?P<param>[^:]+):\s*(?P<desc>.*?)\s*(?:\[(?P<value>[^]]*)\])?\s*(?:\{(?P<group>[^}]*)\})?$

RegEx Demo

RexEx Details:

  • ^: Start
  • :: Match a colon
  • param\s+: Match text param followed by 1+ whitespace characters
  • (?P<param>[^:]+): Match 1+ non-colon characters in named group param
  • :\s*: Match a : followed by 0+ whitespace characters
  • (?P<desc>.*?): Match 0+ of any characters in named group desc
  • \s*: Match 0+ whitespace characters
  • (?:\[(?P<value>[^]]*)\])?: Optionally match [...] and capture inner text in in named group value
  • \s*: Math 0+ whitespace characters
  • (?:\{(?P<group>[^}]*)\})?: Optionally match {...} and capture inner text in in named group group
  • $: End
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.