0

Examples of the data I'm looking to parse:

  • Manufacturer XY-2822, 10-mill, 17-25b
  • Other Manufacturer 16b Part
  • Another Manufacturer WER M9000, 11-mill, 11-40
  • 18b Part
  • Maker 11-36, 10-mill
  • Maker 1x or 2x; Max sizes 1x (34b), 2x (38/24b)

I'm trying to respectively extract:

  • 17, 25
  • 16
  • 11, 40
  • 18
  • 11, 36
  • 34, 38, 24

For the last one, only getting 38, 24 is also acceptable.

So, I may or may not have other numbers in the data, and I may or may not have "b" appended to the item I'm interested in. I also need to extract multiple numbers which are either separated by a dash (-) or a forward slash (/).

Should I be using a parsing expression grammar for this?

Would it be simpler to write the regex, if so, should I write several expressions or can I do this in one fell swoop?

edit adding more cases here, since a good answer falls apart when held to a bit more scrutiny

whistler
  • 767
  • 7
  • 11

1 Answers1

1

I should use positive lookahead.

\d+(?=[^,]*$)

DEMO

Update:

Use the below regex and get the string you want from group index 1 and 2.

(\d+)(?:[\/-](\d+)|b)

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274