我想在Java中開發一個簡單的REGEX而沒有成功。這是我想要完成的。Java正則表達式空emptyString只有數字和。或者,需要的數字
不接受空字符串。分隔符數字的左側或右側至少需要一組數字。
13.
被接受;.13
也被接受。分隔符可能是
.
或,
。只允許一個或零個字符分隔符外觀。
不允許單獨使用不帶數字的分隔符。例如,
.
不允許單獨使用。
我面臨的主要問題是如何使分隔符可選。我想這一點:
private final Pattern pattern = Pattern.compile("^[0-9]*.?|,?[0-9]*$");
這裏我試圖在哪些是可選的字符串的開始到指定號碼的列表。稍後,我指定一個分隔符,它可能是.
或,
,它們也是可選的。後來,右側也是一串數字,也是可選的。這是問題:只有一個.
或一個空字符串匹配。
這裏是我的代碼:
Matcher matcher = clazz.pattern.matcher(input);// a empty string should not be matching
System.out.println(input+" "+matcher.matches());
input="a";
matcher = clazz.pattern.matcher(input);//true how can i fix this letter should not be allowed....
System.out.println(input+" "+matcher.matches());
input="13.";
matcher = clazz.pattern.matcher(input);//returns true is OK.
System.out.println(input+" "+matcher.matches());
input=".13";
matcher = clazz.pattern.matcher(input);//returning false why is this if . is a character separator allowed.
System.out.println(input+" "+matcher.matches());
input="13.,";
matcher = clazz.pattern.matcher(input);//false is OK only one separator might be specify.
System.out.println(input+" "+matcher.matches());
input=",.13";
matcher = clazz.pattern.matcher(input);//false is OK only one separator might be specify.
System.out.println(input+" "+matcher.matches());
input="13,";
matcher = clazz.pattern.matcher(input);//true is OK only one separator might be specify. , in this case
System.out.println(input+" "+matcher.matches());
input=",13";
matcher = clazz.pattern.matcher(input);
System.out.println(input+" "+matcher.matches());//true is ok the separator is , in this case
input="1131313133";
matcher = clazz.pattern.matcher(input);
System.out.println(input+" "+matcher.matches());//true only numbers
input="113.1313.133.13";
matcher = clazz.pattern.matcher(input);
System.out.println(input+" "+matcher.matches());//false is OK only one appeareance of the separator is allowed.
input="1131313133.132313";
matcher = clazz.pattern.matcher(input);
System.out.println(input+" "+matcher.matches());//false why is this... please help
input=".";
matcher = clazz.pattern.matcher(input);
System.out.println(input+" "+matcher.matches()); //true how can i fix this i need a left or right sequence of numbers.
input=".....";
matcher = clazz.pattern.matcher(input);
System.out.println(input+" "+matcher.matches());//false is OK
input=",,,,,,,";
matcher = clazz.pattern.matcher(input);
System.out.println(input+" "+matcher.matches());//false is OK
woooh。你懂了,它就像魅力一樣。非常感謝你的上帝保佑.. – javiut 2013-03-15 16:12:14
@ user1119974記住[upvote和/或接受](http://meta.stackexchange.com/a/168143/206447)這個答案,如果你發現它有幫助。 – Dukeling 2013-03-15 16:15:22
另請注意,對於您可能不希望00000匹配的數字,但這在要求中沒有說明。哇,這將是有益的,你可以再次幫助.. – javiut 2013-03-15 16:21:19