Sample Header Ad - 728x90

Matching Repeating Pattern Using Regex

3 votes
5 answers
15659 views
Let's say I have a file like following 1,2,3-5,6 1,2,3-5,6, 1 1-3 1,2,3-,4,5-7 1,2,3-,4,5-7, 1,2,-3,4,5 1,2,-,3,4 1,2,,,3,4 ,1,2,3 Only combination of following rules should be considered as valid: 1. Ranges [0-9]+-[0-9]+ 2. Groups [0-9]+,[0-9]+ 3. Single Numbers [0-9]+ The lines could ending with comma should also be considered valid I want to extract only 1,2,3-5,6 1,2,3-5,6, 1 1-3 As the other lines shown below do not match the rules 1,2,3-,4,5-7 1,2,3-,4,5-7, 1,2,-3,4,5 1,2,-,3,4 1,2,,,3,4 ,1,2,3 Because some lines have incomplete ranges, some have missing numbers in groups ---- P.S: A PCRE compatible grep only solution would be awesome, but other solutions are also welcome
Asked by GypsyCosmonaut (4289 rep)
Jul 25, 2021, 10:50 AM
Last activity: Sep 11, 2021, 10:13 PM