簡體   English   中英

Java:輸入字段的正則表達式只有數字和逗號+空格字符

[英]Java: Regular expression for the input field only numbers and commas + space characters

有一個字段填充數字或數字組。

逗號后必須是空格!

符號“ , ”和空格不能是字段的開頭和/或結尾

^[\d+]{1,}([,]{1}[\s]{1}).*[\d+]$ 
- this does not work. please help to write a regular expression according to the described condition.

1 - ok!
2, 3 - ok!
6, 7, 4 -ok!
,5 - bad!
5 6 0 - bad!
4,5 - bad!

您可以使用前置空格(或\\s )的重復組。

在你的模式中你可以刪除.*並匹配組內的最后一個\\d+ 然后重復0次以上的組。

它看起來像^[\\d]{1,}([,]{1}[\\s]{1}[\\d]+)*$

請注意,您不必將\\d+放在方括號之間,否則+將按字面匹配,並且可省略量詞{1}

^\d+(?:, \d+)*$

在Java中

String regex = "^\\d+(?:, \\d+)*$";

正則表達式演示 | Java演示

暫無
暫無

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

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