Skip to content
Advertisement

Regex for splitting apparel sizes

I have the following input (only for example, real input contains much more crazy data)

JavaScript

and want to split it by separators like / or , but keep pairs of values. This should be done only, if separator does not occur multiple times, so the result should look like:

JavaScript

What I have so far is

JavaScript

But this fails for 40/42/44/46/48 which returns

JavaScript

But each number should be returned separately. Modifying regex to '(?P<var1>(' . $decimals . ')([/-])(?-2)|(?-2))(?!3)' is better, but still returns wrong result

JavaScript

How should the correct regex look like?

Advertisement

Answer

As stated in comments above, I know that a 100% match is not possible, because of user input. But I’ve found a regex which fits most of my use cases:

JavaScript

See https://regex101.com/r/q3YSa7/1

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement