由分隔符分隔的小數范圍的Java正則表達式

[英]Java regular expression for decimal ranges separated by a delimiter


I have a input field that I want to validate.This input is used to describe a range. The input can take values of the form x-y or x,y . x and y can be decimal numbers like 101.22-135.33 or 101.22,135.33. I am planning to use regular expression to check for the validity of this range field. Can someone provide with the appropriate regular expression?

我有一個我要驗證的輸入字段。此輸入用於描述范圍。輸入可以采用x-y或x,y形式的值。 x和y可以是十進制數,如101.22-135.33或101.22,135.33。我打算使用正則表達式來檢查此范圍字段的有效性。有人可以提供適當的正則表達式嗎?

2 个解决方案

#1


1  

I'm not sure if you want this:

我不確定你是否想要這個:

(\d+\.\d+)[-,](\d+\.\d+)

The code:

String n1 = "125.6,11.2".replaceAll("(\\d+\\.\\d+)[-,](\\d+\\.\\d+)","$1");
String n2 = "125.6,11.2".replaceAll("(\\d+\\.\\d+)[-,](\\d+\\.\\d+)","$2");
System.out.println(n1);
System.out.println(n2);

prints:

125.6
11.2

#2


0  

You can try this regex:

你可以嘗試這個正則表達式:

^\\s*[=-]?\\d+(\\.\\d+)?\\s*(?:[,-]|and)\\s*[=-]?\\d+(\\.\\d+)?$

PS It won't accept .50-.75 but will accept: 0.50-0.75

PS它不接受.50-.75但接受:0.50-0.75


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/11/18/7253d9e5dbaed880da33839744d88e0.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com