2

I have the following string:

Weight ::  1390 - 1430 Sizes :: 1450 - 1480 - 1530 - 1600 - 1700 - 1800 Min :: 1350

I would like to get all values of size. So in this example it would be 1450,1480,1530,1600,1700,1800. The amount of available sizes is variable. It can be between 2 - 10 times.

I have already tried the following pattern:

(?i)sizes :: *(?=.*)(?:(\d*)( - )*) .*min

But then I only get the first value (1450).

If I use this pattern, I have all available values in group1. But then I'm stuck.

(?i)sizes :: *(.*) .*min

Dominic Jonas
  • 4,717
  • 1
  • 34
  • 77
  • 2
    If you are using it in .NET, you may use [`(?i)sizes ::(?: +(?:- +)?(\d+))+ min`](http://regexstorm.net/tester?p=%28%3fi%29sizes+%3a%3a%28%3f%3a+%2b%28%3f%3a-+%2b%29%3f%28%5cd%2b%29%29%2b+min&i=Weight+%3a%3a++1390+-+1430+Sizes+%3a%3a+1450+-+1480+-+1530+-+1600+-+1700+-+1800+Min+%3a%3a+1350) and get all captures in Group 1. You may do the same thing with Python PyPi regex module. In other cases, you will capture all the values as you are doing, and then split Group 1 contents to get separate values. – Wiktor Stribiżew Nov 15 '18 at 20:49
  • Thats weird, if I use your pattern on "regex101.com", I also get only 1 value: https://regex101.com/r/pV2gnv/1 – Dominic Jonas Nov 15 '18 at 20:57
  • 1
    I said it is a .NET pattern, regex101 does not support .NET regex. – Wiktor Stribiżew Nov 15 '18 at 21:06

1 Answers1

1

You can use a negative look behind, like this:

@"(?<=Sizes.*)\d+(?<!Min.*)"

It uses a look behind, looking for 'Sizes' followed by any number of any character.

Then it matches any number of digits.

Finally it uses a negative look behind, looking for 'Min'. That makes it stop matching, when that Word it reached.

Poul Bak
  • 10,450
  • 5
  • 32
  • 57
  • Thanks for your answer! How have you tested your pattern? I got an error: https://regex101.com/r/pV2gnv/2 – Dominic Jonas Nov 15 '18 at 20:59
  • 2
    You can try it here: http://bak-o-soft.dk/RegEx/RegExBuilder.aspx (regex101 does not have a .Net tester). – Poul Bak Nov 15 '18 at 21:00
  • 1
    The pattern is not doing the same thing as the original pattern, e.g. it does not check if `Min` follows the numbers. It will fail with `Min Weight :: 1390 - 1430 Sizes :: 1450 - 1480 - 1530 - 1600 - 1700 - 1800 Min :: 1350` string. It will also match numbers anywhere after `Sizes`, not necessarily after `Sizes ::` – Wiktor Stribiżew Nov 15 '18 at 21:10