2016-12-26 81 views
-1

你好,我是新^0 *正則表達式是不是正則表達式中使用的Java

^0* - 
g 
^ asserts position at start of the string 
0* matches the character 0 literally (case sensitive) 
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy) 
Global pattern flags 
g modifier: global. All matches (don't return after first match) 

絃樂「3454tdfgffg」它應該返回false,因爲沒有零

下面是我的榜樣

public class RegExTest { 

    public static void main(String args[]){ 
      String pattern ="^0*"; 

      String instance = "3454tdfgffg"; 

      // Create a Pattern object 
      Pattern r = Pattern.compile(pattern); 

      // Now create matcher object. 
      Matcher m = r.matcher(instance); 
      if (m.find()) { 
       System.out.println("Available"); 
      }else 
      { 
       System.out.println("Not available"); 
      } 

    } 
} 

,但它始終返回true,什麼都在我的實例變量寫

可以喲你請解決我,我錯了我

回答

4

它將始終返回true。

0*匹配的字符0重複零和無限倍之間。在您的字符串中,此字符丟失,因此這意味着字符0重複零次並且匹配返回true按預期。

如果你需要匹配至少一個零,使用^0+

+0

感謝反斜線:)它; S工作 –

+0

不知道爲什麼這樣的人downvote,我已經返回我沒有多少知識 –

+0

@SiddhpuraAmit我認爲這是因爲它也寫在你的問題中:'*量詞 - 在零和無限次之間匹配,儘可能多次,根據需要回饋(貪婪)'。無論如何,正則表達式可能是困難的,如果你是他們新手,所以我很高興答案:) – BackSlash

相關問題