Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/
1. The string class is immutable
Once a string is created, its content cannot be changed. to modify it, you must create a new string. The stringbuffer class is the same as the string class, but its content can be modified. It is thread-safe. Stringbuilder is basically the same as stringbuffer, but it is lightweight and insecure. They are all in the Java. lang package.
2. Regular Expression in Java
Regular expressions are used in the matcher and pattern classes. They are all used in Java. util. RegEx. You use pattern to create a regular expression and use matcher for matching. In fact, many string methods support regular expression. Note that you must handle the patternsyntaxexception when using regular expression. This exception is thrown when the regular expression syntax is incorrect.
Regular Expressions can be nested. For example, [[ABC] [012] defines a pattern that matches ABC or 012, but it can also be written as [abc012]. In addition, you can use a logical connector for connection. For example, to match all the word characters (all uppercase and lowercase letters, numbers 0-9, and underscores) but do not contain uppercase letters, can write [/W & [^ A-Z].
Note that if the regular expression in Java uses escape characters, // is used to represent a/. For example,/B/W +/B must be written in Java: // B // W + // B
There are three types of matching.
The first one is greedy, which is the example above. It matches the longest part and tries to search the entire input string that has been read (or eaten) before the first match. If the first matching attempt (the entire input string) fails, the matcher returns a character from the input string and tries again to repeat the process, wait until a match is found or no more remaining characters can be removed.
While reluctant is barely able to match, also known as lazy. The matching is shortest. How can it be used? Create them in the opposite way: they start from the beginning of the input string, and then gradually read a character for search and match at a time. The final content they try to match is the entire input string.
The third is possessive, which is similar to greedy. The difference is that it is not smart enough, when it finds that it does not match all the characters that can be eaten by a single bite, it considers that the entire string does not match, and it does not try to spit out a few. Its behavior is similar to that of a large landlord. It is greedy but stupid. Create with +.
For example, if "simple sample" is used, the entire string is matched with the pattern S. + E. Among them,. Is any character other than line breaks, while + represents one or more, and. + represents one or more of them except line breaks. This is greedy.
S. +? E will match simple, which matches the shortest string.
S. ++ e does not match any string, because. ++ matches all characters After S, which makes e unknown and causes matching failure.
Group applications:
I had one dog, but he had two dogs.
She had a cat, but did she want more cats?
She also has a dog. But his dogs were bigger.
We want to match the singular and plural nouns, but we do not want to match them in two sentences. For example, we match dog, dogs, cat, and cats, but the third sentence is a match between two sentences. So we do not match that sentence. How to write it?
We use group:/B (/W +)/B [^.?!] *? /1 s
/B is the character boundary, and (/W +) represents a group, which contains a word. [^.?!] Does not contain punctuation marks (. Or? Or !). *? Indicates repeated times, but as few times as possible. /1 indicates the group, and s indicates the plural form of the word.
Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/