最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Validating Thickness (MarginPadding) string using Regex for different platforms - Stack Overflow

programmeradmin1浏览0评论

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}$

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}$

Share Improve this question asked Apr 1 at 12:23 programmerboyprogrammerboy 4296 silver badges15 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 2

You are matching an optional , and an optional ; on every repetition, which makes the regex also match strings like 123123123123 123123123123, 123123123123,;


  • You could write the regex starting with 1-3 digits
  • Then optionally repeat a grouped part where the comma is mandatory
  • Then optionally repeat that whole part again where the semicolon is mandatory

For example:

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

The regex in parts matches:

  • ^ Start of string
  • \d{1,3} Match 1-3 digits
  • (?:,\d{1,3}){0,3} Repeat 0-3 times matching , and 1-3 digits
  • (?: Non capture group to repeat as a whole part
    • ; \d{1,3} Match ; and 1-3 digits
    • (?:,\d{1,3}){0,3} Repeat 1-3 times matching , and 1-3 digits
  • )* Close the non capture group and optionally repeat it
  • $ End of string

See a regex 101 demo


If you want to match either 1, 2 or 4 numbers separated by a comma:

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

See another regex 101 demo

If you want to match optional spaces around the semicolon, then you could use *; * or just a ; if there should be no spaces.

A more broader match is using \s*;\s* but note that is could also possibly match newlines.

This is probably an additional way.

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

https://regex101/r/o8cj4Z/1

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

评论列表(0)

  1. 暂无评论