Java Regular Expression Application summary

Source: Internet
Author: User
Tags character classes

http://lavasoft.blog.51cto.com/ http://lavasoft.blog.51cto.com/62575/179324 Java Regular Expression application summary I. OverviewRegular expressions are an important tool for Java to handle strings and text.Java's handling of regular expressions is concentrated in the following two two classes:Java.util.regex.Matcher Mode class: Used to represent a compiled regular expression. Java.util.regex.Pattern Match class: matches the abstract result expressed by a string with a pattern. (Unfortunately, Java Doc does not give the concept of responsibility for these two classes.) )For example, a simple example:ImportJava.util.regex.Matcher;
ImportJava.util.regex.Pattern;

/**
* Regular Expression Example
*
* @author leizhimin 2009-7-17 9:02:53
*/
PublicclassTESTREGX {
PublicStaticvoidMain (string[] args) {
Pattern p = pattern.compile ("f (. +?) K ");
Matcher m = P.matcher ("FCKFKKFKF");
while(M.find ()) {
String S0 = M.group ();
String S1 = m.group (1);
System.out.println (S0 +"||"+ S1);
}
System.out.println ("---------");
M.reset ("fucking!");
while(M.find ()) {
System.out.println (M.group ());
}

Pattern P1 = Pattern.compile ("f (. +?) I (. +?) H ");
Matcher m1 = P1.matcher ("Finishabigfishfrish");
while(M1.find ()) {
String S0 = M1.group ();
String S1 = m1.group (1);
String s2 = m1.group (2);
System.out.println (S0 +"||"+ S1 +"||"+ s2);
}

System.out.println ("---------");
Pattern P3 = Pattern.compile ((19|20) \\d\\d ([-/.]) (0[1-9]|1[012]) \\2 (0[1-9]|[ 12][0-9]|3[01]) ");
Matcher m3 = P3.matcher ("1900-01-01 2007/08/13 1900.01.01 1900 1900-01.01 1900");
while(M3.find ()) {
System.out.println (M3.group ());
}
}
Output Result: fck| | C
fkk| | K
---------
Fuck
finish| | in| | S
fishfrish| | ishfr| | S
---------
1900-01-01
2007/08/13
1900.01.01
1900 01 01
1900 02 31

Process finished with exit code 0 second, some easy to confuse problems1, Java on the problem of anti-slash processingin other languages, \ \ Indicates that you want to insert a character \;in the Java language, \ \ represents a backslash to insert a regular expression, and subsequent characters have a special meaning. Look at the API documentation: predefined character classes. Any characters (which might or may not match the line terminator) \d number: [0-9]\d non-numeric: [^0-9]\s whitespace character: [\t\n\x0b\f\r]\s non-whitespace character: [^\s]\w word character: [a-za-z_0-9]\w non-word character: [^\w]but look at the above procedure, the comparison is not difficult to see:\d in the actual use of the time it was written\\d;In a Java regular expression, if you want to insert a \ character, you need to write it in a regular expression\\\\, because the following Apidoc definition \ \ represents a backslash. However, you do not need to add backslashes more if you are in a regular expression, such as carriage return line. For example, enter \ r to write \ r.Character x character x\\ backslash character \0n with octal value 0 characters n (0 <= n <= 7) \0nn characters with octal value 0 nn (0 <= n <= 7) \0mnn with octal value 0 character mnn (0 &lt = M <= 3, 0 <= n <= 7) \xhh character with hexadecimal value 0x hh\uhhhh character hhhh\t with hexadecimal value 0x tab (' \u0009 ') \ n New Line (newline) character (' \u000a ') \ r Enter Character (' \u000d ') \f (' \u000c ') the \a alarm (' \u0007 ') \e escape character (' The ' ' \u001b ') \cx the Control 2, Matcher.find () that corresponds to x: Attempts to find the next child of a sequence of characters matching the pattern Sequence. This method starts at the beginning of the character sequence, if the previous invocation of the method succeeds and since then the match is not reset, starting with the first character that did not match the previous match operation, that is, if a subsequence that matches the pattern was first found this time from the subsequence. 3. Matcher.matchers (): Determines whether the entire character sequence matches the pattern. When checking multiple strings with a Matcher object continuously, you can use Matcher.reset (): Reset the match, discard all of its explicit state information, and set its add location to zero. or Matcher.reset (charsequence input) resets this match with the new input sequence. To re-use the matching device. 4, the concept of the group is very important, the group is a regular expression with parentheses, you can refer to the group by number.The group number starts with 0, there are several pairs of parentheses representing several groups, and the group can be nested, the group number is 0 for the entire expression, the group number is 1 for the first group, and so on. For example, there are three groups in a (B) C (d) E regular formula, group 0 is ABCDE, Group 1 is B, Group 2 is D;a ((b) c) (d) E regular formula has four groups: group 0 is ABCDE, group 1 is BC, Group 2 is B, Group 3 is C, and Group 4 is D. int GroupCount (): Returns the number of groups that match their pattern, excluding group No. 0. String Group (): Returns the No. 0 Group of the previous match operation, such as find (). String Group: Returns a sub-sequence that matches the specified group during the previous match operation. If the match succeeds, but the specified group fails to match any part of the character sequence, NULL is returned. int start (int group): Returns the initial index of a subsequence that matches the specified group during the previous match operation. int end (int group): Returns the last index of the subsequence that matches the specified group during the previous match operation +1. 5, the matching range of control of the most abnormal is to calculate the Lookingat () method,The name is confusing., need to look carefully apidoc.  start ()   Returns the initial index that was previously matched. End ()   Returns the offset after the last matching character.  public Boolean Lookingat () attempts to match the input sequence starting at the beginning of the zone with the pattern. Like the matches method, this method always starts at the beginning of the range, and unlike it, it does not need to match the entire region. If the match succeeds, you can get more information through the start, end, and group methods. Returns: Returns True when and only if the prefix of the input sequence matches the pattern of this match. static method of  6, pattern tag  pattern class static Pattern compile (String regex, int flags)            compiles the given regular expression into the pattern with the given flags. The flags parameter is the pattern tag, which is important at some point.  Pattern.CANON_EQ          enable specification equivalence. pattern.case_insensitive          enables case-insensitive matching. Whitespace and annotations are allowed in pattern.comments          mode. pattern.dotall          Enable Dotall mode. pattern.literal          the literal value analysis of the enabled mode. pattern.multiline          enable multi-line mode. Pattern.unicode_case          enables Unicode-aware case folding. pattern.unix_lines          Enable UNIX line mode.   three, substitution of strings  string.replace (char OldChar, char Newchar)           returns a new string, It is generated by replacing all the oldChar that appear in this string with Newchar. String.Replace (charsequence target, charsequence replacement)            replaces this string with each substring of the literal target sequence, using the specified literal substitution sequence. String.replaceall (string regex, string replacement)           using the given The replacement string replaces this string with each substring of the given regular expression. String.replacefirst (string regex, string replacement)           using the given Replacement string substitution This string matches the first substring of a given regular expression.  stringbuffer.replace (int start, int end, String str)           Replaces the characters in the substring of this sequence with the characters in the given string. Stringbuilder.replace (int, int, java.lang.String)           using the given The character in string replaces the character in the substring of this sequence.  matcher.replaceall (string replacement)           Replace mode with the given replacement stringEach sub-sequence of the input sequence that matches. Matcher.replacefirst (String replacement)           Replaces the first subsequence of an input sequence that matches a given replacement string.   Four, the segmentation of the stringString[] Split (string regex) splits this string based on the match of the given regular expression.  String[] Split (string regex, int limit) splits this string by matching a given regular expression. Of course, there is also a StringTokenizer class that can be used to slice strings, but now sun is deprecated. Change the idea, in fact, with regular expression can also achieve the purpose of dividing the string into segments. v. No mention ofThe API of regular expression is simple and easy to use, not too many complicated places, not unimportant, the most difficult problem of regular expression is proficiency in writing regular expressions. The specification of regular expressions, in the pattern class Apidoc is very detailed introduction, and clear, here is not to repeat.

Java Regular Expression Application summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.