Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating Thickness (Margin/Padding) string using Regex for different platforms

I have a specific Regex pattern requirement for a string pattern which contains margin/padding (Thickness) for max 3 different platform and devices separated by semicolon. Margin and Padding format is for .NET MAUI where you can have Universal, Horizontal/Vertical, LTRB.

Comma separated is Thickness (Margin) and semicolon separator is for different devices/platforms. Note that end of string shouldn't end with comma or semicolon.

Should Match (spaces are intentional for visibility)

  • 10
  • 10,10 ; 5,5
  • 10,11,12,13 ; 20,30 ; 50

Shouldn't Match (spaces are intentional for visibility)

  • 10 ,
  • 10,10,999 ; 10,10 ,;
  • 10,10 ; , 10;10,10
  • 10 ;; 10

Tried this myself, but couldn't achieve above.

^((?:\d{1,3},?){1,4};?){1,3}$

like image 227
programmerboy Avatar asked Oct 16 '25 16:10

programmerboy


1 Answers

This is probably an additional way.

^\b(?!.*\d{4})(?:(?:^|;)\d+(?:,\d+){0,3}){1,3}$

https://regex101.com/r/jW5r6Z/1

^ \b 
(?! .* \d{4} )                # No 4 or more digits
(?:
   (?: ^ | ; )                # BOS or device separator
   \d+                           # 1-4 Margins
   (?: , \d+ ){0,3}
){1,3}                        # 1-3 Devices
$ 

Info on the Guard assertion used:

Margin is digits and allowed 1-3 digits per margin. Allowed margins per device is 1-4. The margin regex can be expressed then as \d{1,3} (?: , \d{1,3} ){0,3} where this represents 1-4 margins.

The valid digits are quantified as \d{1,3} allowed in a group. Doing the math, there can not be 4 or more digits. The digits can then be refactored by setting an assertion at the beginning of the string that finds a group of 4 digits. If found the string is invalid. If not, any digits found are controlled
by simple quantifiers + of * where no block is greater than 3 digits.

This allows to convert \d{1,3} into \d+ which is much more efficient.

Edited Rx to allow only 1 or 2 or 4 margins
This mod just goes from 1-4 margins to a stricter 1, 2 or 4 mods.
So a 3 mods will not match. Just a slight alternation is needed to implement it this way.

^\b(?!.*\d{4})(?:(?:^|;)\d+(?:(?:,\d+){1}|(?:,\d+){3})?){1,3}$

https://regex101.com/r/SbwKj5/1

^ \b
(?! .* \d{4} )              # No 4 or more digits
(?:
   (?: ^ | ; )                # BOS or device separator
   \d+                           # 1 margin  plus
   (?:                           # Optional
      (?: , \d+ ){1}                # 1 margin = 2 total ,
    | (?: , \d+ ){3}                # or 3 margin = 4 total
   )?
){1,3}                        # 1-3 Devices
$ 

Also re: (?:^|;) # BOS or device separator
BOS stands for beginning of string ^
The ; is the device separator between margins where the regex will match 1-3 of them.

like image 83
sln Avatar answered Oct 18 '25 05:10

sln



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!