2016-12-26 149 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

相关问题