Regex handling zero
I have string with *(asterisk) symbols as an input. String is considered as invalid if it has two consecutive asterisks. But, there is an escape symbol (backslash).
For example:
I'm on stuck on such regex's which produce incorrect result:
/[^]**/
- java.util.regex.Pattern.compile("/[^\]**/")
/([^]*?**)|(**)/
- java.util.regex.Pattern.compile("/([^\]*?**)|(**)/")
. Also, I've read about greedy, reluctant and possessive quantifies from here http://docs.oracle.com/javase/tutorial/essential/regex/quant.html
I know that problem is about zero-length matches, but could not produce correct regex.
Are you looking for a regex, that will only match invalid strings? This should do:
"(?<!\)**+"
It will match two or more asterisks in a row, not preceded by a backslash.
EDIT: (?<!foo)
thingy is called "negative look-behind". It matches any zero-length place in the string that is not immediately preceded by a region matching the regex inside parentheses ("foo" in this case, or a backslash in yours). I had this as [^\]
at first, which is almost the same thing (in this case), except that it matches any character, other than a backslash, but not an absense of a character, like at the beginning of a string in "**".
There is a good detailed description of lookarounds (look-behind and look-ahead) as well as a lot of other regex "magic" here
Use string.matches method. This returns true for valid strings.
String s1 = "case**";
String s2 = "case**";
System.out.println(s1.matches("(?=.*(\**|*\*)).*"));
System.out.println(s2.matches("(?=.*(\**|*\*)).*"));
Output:
false
true
DEMO
链接地址: http://www.djcxy.com/p/76970.html上一篇: 正则表达式贪婪匹配以字符或字符串结尾结束
下一篇: 正则表达式处理零