正则表达式

来源:互联网 发布:pymongo 删除数据 编辑:程序博客网 时间:2024/05/02 03:06

       正则表达式(regular expression)用于指定字符串模式,可以在任何需要定位匹配某种特定模式的字符串的情况下使用正则表达式。

       grep, Perl, Tcl, Python, PHP, and awk都提供了正则表达式的功能特性,但各自语法都不尽相同,java.util.regex和Perl语法最为相似。java.util.regex package 主要有三个类组成: Pattern,Matcher and PatternSyntaxException.

----A Pattern object is a compiled representation of a regular expression. The Pattern class provides no public constructors. To create a pattern, you must first invoke one of its public static compile methods, which will then return a Pattern object. These methods accept a regular expression as the first argument;

----A Matcher object is the engine that interprets the pattern and performs match operations against an input string. Like the Pattern class, Matcher defines no public constructors. You obtain a Matcher object by invoking the matcher method on a Pattern object.

----A PatternSyntaxException object is an unchecked exception that indicates a syntax error in a regular expression pattern.

 
metacharacters(通配符)
java正则表达式支持的有: < ( [ { \ ^ - = $ ! | ] } )  ? * + . >。有两种方法可以强制让通配符被作为普通字符处理:
  • precede the metacharacter with a backslash, or
  • enclose it within \Q (which starts the quote) and \E (which ends it).
       在java的正则表达式中,\\的意思是“我要插入一个正则表达式的反斜线,所以其后的字符具有特殊的意义。”例如,如果你想表示一位数字,那么正则表达式应该是\\d。如果你想插入一个普通的反斜线,则应该这样\\\\。不过换行和制表符之类(总称空白符\s)的东西只需要使用单反斜线:\n\t。

character classes(字符串类)

这篇文章阐述了反向引用。
http://blog.csdn.net/aspirinvagrant/article/details/48949047

正则表达式的0长度匹配???????????????


Differences Among Greedy, Reluctant, and Possessive Quantifiers

There are subtle differences among greedy, reluctant, and possessive quantifiers.

Greedy quantifiers are considered "greedy" because they force the matcher to read in, or eat, the entire input string prior to attempting the first match. If the first match attempt (the entire input string) fails, the matcher backs off the input string by one character and tries again, repeating the process until a match is found or there are no more characters left to back off from. Depending on the quantifier used in the expression, the last thing it will try matching against is 1 or 0 characters.

The reluctant quantifiers, however, take the opposite approach: They start at the beginning of the input string, then reluctantly eat one character at a time looking for a match. The last thing they try is the entire input string.

Finally, the possessive quantifiers always eat the entire input string, trying once (and only once) for a match. Unlike the greedy quantifiers, possessive quantifiers never back off, even if doing so would allow the overall match to succeed.

To illustrate, consider the input string xfooxxxxxxfoo.

 Enter your regex: .*foo  // greedy quantifierEnter input string to search: xfooxxxxxxfooI found the text "xfooxxxxxxfoo" starting at index 0 and ending at index 13.Enter your regex: .*?foo  // reluctant quantifierEnter input string to search: xfooxxxxxxfooI found the text "xfoo" starting at index 0 and ending at index 4.I found the text "xxxxxxfoo" starting at index 4 and ending at index 13.Enter your regex: .*+foo // possessive quantifierEnter input string to search: xfooxxxxxxfooNo match found.

The first example uses the greedy quantifier .* to find "anything", zero or more times, followed by the letters "f" "o" "o". Because the quantifier is greedy, the .* portion of the expression first eats the entire input string. At this point, the overall expression cannot succeed, because the last three letters ("f" "o" "o") have already been consumed. So the matcher slowly backs off one letter at a time until the rightmost occurrence of "foo" has been regurgitated, at which point the match succeeds and the search ends.

The second example, however, is reluctant, so it starts by first consuming "nothing". Because "foo" doesn't appear at the beginning of the string, it's forced to swallow the first letter (an "x"), which triggers the first match at 0 and 4. Our test harness continues the process until the input string is exhausted. It finds another match at 4 and 13.

The third example fails to find a match because the quantifier is possessive. In this case, the entire input string is consumed by .*+, leaving nothing left over to satisfy the "foo" at the end of the expression. Use a possessive quantifier for situations where you want to seize all of something without ever backing off; it will outperform the equivalent greedy quantifier in cases where the match is not immediately found.

原创粉丝点击