2013-03-15 39 views
0

我想在Java中開發一個簡單的REGEX而沒有成功。這是我想要完成的。Java正則表達式空emptyString只有數字和。或者,需要的數字

  1. 不接受空字符串。分隔符數字的左側或右側至少需要一組數字。 13.被接受; .13也被接受。

  2. 分隔符可能是.,

  3. 只允許一個或零個字符分隔符外觀。

  4. 不允許單獨使用不帶數字的分隔符。例如,.不允許單獨使用。

我面臨的主要問題是如何使分隔符可選。我想這一點:

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 

回答

3

一個問題,除其他外,是因爲運算符優先級的

^[0-9]*.?|,?[0-9]*$ 

是基本相同

(^[0-9]*.?)|(,?[0-9]*$) 

所以digits.digits將不匹配。

[0-9]*表示零或更多,因此只有.會匹配。

另請注意,^$在這種情況下實際上不是必需的,它更適用於當您使用find()或類似的情況。

你想要什麼可能是更多的東西一樣:

[0-9]+[.,]?|[0-9]*[.,][0-9]+ 

[.,]意味着要麼.,
[0-9]+意味着一個或更多數字。
似乎你熟悉其他語法。

Reference

編輯:

另請注意,號碼,你可能不希望00000匹配,但在需求沒有說明。因此,像:

(0|[1-9][0-9]*)[.,]?|(0|[1-9][0-9]*)?[.,][0-9]+ 

上述將匹配任何一個0或任意數量的不以零開始到可選分隔符左側(或什麼,如果有什麼東西在分離器的右側)。

+0

woooh。你懂了,它就像魅力一樣。非常感謝你的上帝保佑.. – javiut 2013-03-15 16:12:14

+2

@ user1119974記住[upvote和/或接受](http://meta.stackexchange.com/a/168143/206447)這個答案,如果你發現它有幫助。 – Dukeling 2013-03-15 16:15:22

+0

另請注意,對於您可能不希望00000匹配的數字,但這在要求中沒有說明。哇,這將是有益的,你可以再次幫助.. – javiut 2013-03-15 16:21:19

相關問題