0

I'm struggling a little with my regex. It's matching but I would like to have between 1 and 3 repeating patterns. The variation of strings I am looking to match against:

D1000_U400_Mbps_TC4_P, D2_U2_Mbps_TC1_C, D0.5_U2.2_Mbps_TC1_C
D0.5_U2.2_Mbps_TC1_C
D2_U2_Mbps_TC1_C,D250_U50_Mbps_TC4_P
D25-50_U5-10_Mbps_TC3_C
Home Superfast
FWPlus_TC3_C, D0.5_U0.1_Mbps_TC3_P

Context:

  • These strings represent different broadband speed types (D - Download speed, U - Upload speed, Mbps - Megabits per second, TC - Traffic Class (between 1-4), C or P (priority indicators)
  • Home Superfast or FWPlus is an "abstract" representation of an entire string or part of a string: example: Home Superfast may be: D1000_U500_Mbps_TC1_P, whereas FWPlus_TC3_C may be: D100_U50_TC3_C (or may represent something else, but that's not important)

Note:

  • Each line represents a possible string (I would only ever be matching on 1 line, not multiple lines)
  • Comma-separated parts (minimum number of parts 1, maximum 3)
  • there may or may not be a space between each part and the comma

Variations:

  • D[number]_U[number]_Mbps_TC[number]_[letter]
  • [text]_TC[number]_[letter]
  • [text]
  • D[number]-[number]_U[number]-[number]_Mbps_TC[number]_[letter]

My regex (which probably needs work):

((?!=,)((Home (Fast|Superfast|Ultrafast))|((FWPlus)+|((D([0-9.]|\-[0-9])+)\_(U([0-9.]|\-[0-9])+)\_(Mbps)))\_(TC[1-4])\_([P,C])))*

I can match, but I would like to match a pattern that is between 1-3 repeats, and I'm not sure if I got the first part right (?!=,)

How can I do that?

RegExr link: https://regexr.com/59att

Sash
  • 1,134
  • 11
  • 23
  • Firstly there are some simplifications Rather than [0-9] -> \d (unless only ASCII digits should be matched \-[0-9] can be written as [0-9-] or of course [\d-] _ does not need to be escaped The quantifier {1,3} on the appropriate group matches 1 to 3 times – TonyR Jul 30 '20 at 05:26
  • thanks @TonyR. is my first part that captures commas correct? – Sash Jul 30 '20 at 05:37
  • What do you want to accomplish? Do you just want to match the occurrence or select fields. – TonyR Jul 30 '20 at 07:25
  • Try it like this `^(?:D\d+(?:\.\d+)?(?:-\d+(?:\.\d+)?)?_U\d+(?:\.\d+)?(?:-\d+(?:\.\d+)?)?_Mbps_TC\d+_[A-Z]|Home (?:Fast|(?:Super|Ultra)fast)|FWPlus_TC\d+_[A-Z])(?:, ?(?:D\d+(?:\.\d+)?(?:-\d+(?:\.\d+)?)?_U\d+(?:\.\d+)?(?:-\d+(?:\.\d+)?)?_Mbps_TC\d+_[A-Z]|Home (?:Fast|(?:Super|Ultra)fast)|FWPlus_TC\d+_[A-Z])){0,2}$` https://regex101.com/r/GiOSSD/1 – The fourth bird Jul 30 '20 at 08:05

0 Answers0