I want to capture all the strings from multi lines data. Supposed here the result and here’s my code which does not work.
Pattern: ^XYZ/[0-9|ALL|P] I’m lost with this part anyone can help?
Result
XYZ/1
XYZ/1,2-5
XYZ/5,7,8-9
XYZ/2-4,6-8,9
XYZ/ALL
XYZ/P1
XYZ/P2,3
XYZ/P4,5-7
XYZ/P1-4,5-7,8-9
>Solution :
The pattern could be:
^XYZ\/(?:ALL|P?[0-9]+(?:-[0-9]+)?(?:,[0-9]+(?:-[0-9]+)?)*)$
The pattern in parts matches:
^Start of stringXYZ\/MatchXYX/(You don’t have to escape the/depending on the pattern delimiters)(?:Outer on capture group for the alternativesALLMatch literally|OrP?Match an optionalP[0-9]+(?:-[0-9]+)?Match 1+ digits with an optional-and 1+ digits(?:Non capture group to match as a whole,[0-9]+(?:-[0-9]+)?Match,and 1+ digits and optional-and 1+ digits
)*Close the non capture group and optionally repeat it
)Close the outer non capture group$End of string