regexpcresplunk

Regex extraction from Right to Left


I have some data where I like to extract data from right to left. Sample data

1,4,34
5,15
22

Expected output:

One=34  Two=4  Three=1
One=15  Two=5
One=22

This is as far as I have got with my regex experience.

(?:(?<three>\d+),)?(?:(?<two>\d+),)?(?<one>\d+)$

But this gives :

One=34  Two=4  Three=1
One=15  Three=5
One=22

So it fails when there are only two extraction. Any good idea? PS I do not have any revers tools


Solution

  • You can make the first 2 groups optional as a whole:

    ^(?:(?:(?<three>\d+),)?(?<two>\d+),)?(?<one>\d+)$
    

    The pattern matches:

    Regex demo