Java \ Pattern - 如何编写一个验证缺少字符串的模式? [英] Java \ Pattern - how to write a pattern that verifies the lack of a string?
问题描述
我试过 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 和圆形括号分组
- regular-expressions.info/Repetition and Round Brackets for Grouping
-
String
class有以下简单的非正则表达式方法:
- The
String
class has the following simple non-regex methods:boolean startsWith(String prefix)
boolean endsWith(String suffix)
boolean contains(CharSequence s)
- 另请参阅: JLS 15.15.6逻辑补语操作员!
- 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
参考文献
- regular-expressions.info/Lookarounds
- regular-expressions.info/Lookarounds
- 正则表达式匹配1234,1324,2341({1,2,3,4}的所有排列)
- 检查每个子串四个零之后是至少四个使用正则表达式
- 正则表达式如何
(?< =#)[^#] +( ?=#)
工作? - 为什么需要否定正则表达式?
- Regex to match 1234, 1324, 2341 (all permutations of {1,2,3,4})
- Checking if every substring of four zeros is followed by at least four ones using regex
- How does the regular expression
(?<=#)[^#]+(?=#)
work? - Why is negation of a regex needed?
-
^(?!agg)。* $
(见rubular.com )
- 这个匹配不以
agg开头的字符串
^(?!agg).*$
(see on rubular.com)- This matches strings that doesn't start with
agg
- 这匹配不包含
agg的字符串
- This matches strings that doesn't contain
agg
这篇关于Java \ Pattern - 如何编写一个验证缺少字符串的模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- This matches strings that doesn't start with
- 这个匹配不以
References
如果您坚持使用负面外观,那么您可以根据实际需要使用这两种模式中的一种:
If you insist on using negative lookarounds, then you can use one of these two patterns depending on what you actually need:
- The
References
原始规格不是很清楚,但这里有一些基本事实:
The original specification isn't very clear, but here are some basic facts: