Java \ Pattern - 如何编写一个验证缺少字符串的模式? [英] Java \ Pattern - how to write a pattern that verifies the lack of a string?

查看:95
本文介绍了Java \ Pattern - 如何编写一个验证缺少字符串的模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过 http: //java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html

我有

Pattern PATTERN = Pattern.compile("agg{0}.*");
Matcher m = PATTERN.matcher("agg_0_4_Jul_2010_13_32_53_759_0.csv");

if (m.matches() == true)  => true.

我希望它返回FALSE - 因为它确实包含字符串start中的agg。
简而言之 - 如何验证缺少子串(以积极的方式)
谢谢。

I want this to return FALSE - since it does contain agg in the string start. in short - how to verify the lack of substring (in a positive way) Thanks.

推荐答案

关于原始模式的注释



您的原始模式包含非常特殊的 agg {0} 。需要说的是,这种模式毫无意义。由于连接和重复之间的优先顺序,以及 {0} 与模式完全重复为零的事实,此 agg {0} 只是 ag

Note on original pattern

Your original pattern contains the very peculiar agg{0}. It needs to be said that this pattern makes no sense. Due to the way precedence between concatenation and repetition, and the fact that {0} is exactly zero repetition of a pattern, this agg{0} is simply ag.

因此,您得到以下结果:

Thus, you get the following:

Pattern PATTERN = Pattern.compile("agg{0}.*");
Matcher m = PATTERN.matcher("aged gouda yum yum!");
System.out.println(m.matches()); // prints "true"

为了说明重复和连接如何相互作用,以及有时需要分组,这里有一些例子:

To illustrate how repetition and concatenation interacts, and how sometimes grouping is required, here are some more examples:

System.out.println(  "hahaha".matches("ha{3}")    ); // prints "false"
System.out.println(  "haaa".matches("ha{3}")      ); // prints "true"
System.out.println(  "hahaha".matches("(ha){3}")  ); // prints "true"



参考文献




  • regular-expressions.info/Repetition 圆形括号分组

  • References

    • regular-expressions.info/Repetition and Round Brackets for Grouping
    • 原始规格不是很清楚,但这里有一些基本事实:

      The original specification isn't very clear, but here are some basic facts:


      • String class有以下简单的非正则表达式方法:

        • The String class has the following simple non-regex methods:
          • boolean startsWith(String prefix)
          • boolean endsWith(String suffix)
          • boolean contains(CharSequence s)
          • See also: JLS 15.15.6 Logical Complement Operator !

          以下是一些简单的例子:

          Here are some simple examples:

          System.out.println(   "Hello world!".startsWith("Hell")  ); // "true"
          System.out.println(   "By nightfall".endsWith("all")     ); // "true"
          System.out.println(   "Heaven".contains("joy")           ); // "false"
          
          System.out.println( ! "Hello world!".startsWith("Hell")  ); // "false"
          System.out.println( ! "By nightfall".endsWith("all")     ); // "false"
          System.out.println( ! "Heaven".contains("joy")           ); // "true"
          






          负面看法



          如果Java的逻辑补码和 String 的非正则表达式谓词检查的组合对你不起作用,可以使用负面外观来否定模式上的匹配。


          On negative lookaround

          If the combination of Java's logical complement and String's non-regex predicate checks don't work for you, you can use negative lookarounds to negate a match on a pattern.

          一般来说,如果你想否定 ^模式$ 匹配,并且由于某种原因你需要这样做在正则表达式本身,您可以匹配 ^(?! pattern $)。* 而不是(可能使用单行模式,因此点匹配所有内容)。

          Generally speaking, if you want to negate what ^pattern$ matches, and for some reason you need this done in the regex itself, you can match on ^(?!pattern$).* instead (perhaps using the single-line mode so the dot matches everything).

          以下是匹配 a * b * 的示例,并使用否定前瞻来否定它:

          Here's an example of matching a*b*, and negating it using negative lookahead:

              String[] tests = {
                  "aaabb",
                  "abc",
                  "bba",
                  "aaaa",
                  "bbbbbb",
                  "what is this?",
              };
              for (String test : tests) {
                  System.out.printf("[%s] %s - %s %n",
                      test,
                      test.matches("a*b*"),
                      test.matches("(?!a*b*$).*")
                  );          
              }
          

          以上打印:

          [aaabb] true - false 
          [abc] false - true 
          [bba] false - true 
          [aaaa] true - false 
          [bbbbbb] true - false 
          [what is this?] false - true 
          



          参考文献



查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆