簡體   English   中英

用於驗證分隔符分隔數字的正則表達式

[英]Regular expression to validate delimiter separated numbers

我需要一個正則表達式來驗證具有這些值的表單的輸入

例如:

123, 444,231, 2231 ...

正則表達式在該循環中的最佳方式是什么?

如果我正確理解了您的問題,您想匹配數字嗎?

用這個:

([0-9]{1,}+){1,}

那將匹配每個值。

我可以建議您以下。 ^(\\s*\\d+\\s*,?)+$會考慮CSV值的前導和尾隨空格的可能性

編輯

我修改了表達式,這是新版本: ^(\\s*\\d+\\s*,)*(\\s*\\d+\\s*){1}$在注釋情況下不會失敗。

([1-9]\d{2,3},\s*)  //first one digit 1-9, followed by 2 to 3 other digits,a comma and zero or one whitespace
{0,}            //This pattern may occour 0 or more times, followed by...
([1-9]\d{2,3})  // one of these number without the trailing comma, only once.

需求說明有點含糊,但這可能是您想要的:

^(\s*\d+\s*(?(?=,\s*\d),|))+$

它驗證以“,”分隔的數字序列,允許在開頭和結尾的白色字符,而不以“,”開頭和結尾

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM