Search code examples
regexregular-language

how to make a regular expression for this?


I want to make a regular expression on the string "{{c1::tiger}} is a kind of {{c2::animal::something movable}}" to get the word "tiger" and "animal",and I have made this expression \{\{c\d+::((?P<value>.*?)(:{0,2})(.*?))\}\},also I want to use group('value') to achieve this.The result word "tiger" is exactly what I need,but always get the wrong result "animal::something movable"(which I mean "animal"),could anyone help me to solve this problem?Thanks.


Solution

  • The pattern that you tried contains 4 capturing groups and for the current example data group 1 and group 3 are empty.

    To get tiger you could use a single capturing group with a negated character class:

    \{\{c\d+::(?P<value>.*?)(?:::|}})
    

    Regex demo

    If the closing }} have to be present, you could use:

    \{\{c\d+::(?P<value>.*?)(?:::.*)?}}
    

    Regex demo