I tried from https://docs.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html.
I have
Pattern PATTERN = Pattern.compile(\"agg{0}.*\");
Matcher
Simple alternative:
if (!csv.startsWith("agg") {
// do something
}
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"
The original specification isn't very clear, but here are some basic facts:
!
unary boolean
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"
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
If you insist on using negative lookarounds, then you can use one of these two patterns depending on what you actually need:
^(?!agg).*$
(see on rubular.com)
agg
^(?!.*agg).*$
(see on rubular.com)
agg
Check contains , it's a method of String