tags:

views:

71

answers:

3

I tried from http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html

I have

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.

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.

A: 

Check contains , it's a method of String

Alberto Zaccagni
+1  A: 

Simple alternative:

if (!csv.startsWith("agg") {
  // do something
}
Andreas_D
+6  A: 

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"

References


On negating a matching

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

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"

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.

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).

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*$).*")
        );          
    }

The above prints:

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

References

Related questions


Going back to the question

If you insist on using negative lookarounds, then you can use one of these two patterns depending on what you actually need:

polygenelubricants