java pattern 验证,Java \ Pattern - 如何编写一个验证缺少字符串的模式?

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.

解决方案

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:

^(?!agg).*$ (see on rubular.com)

This matches strings that doesn't start with agg

^(?!.*agg).*$ (see on rubular.com)

This matches strings that doesn't contain agg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值