CC is often used to filter the data with regular expressions, when the data washing is more complex, more rules. This time do Leetcode, review Java's regular match. Leetcode 537. Complex number multiplication takes the real and imaginary parts out of the string representing the plural.
http://blog.csdn.net/yin380697242/article/details/52049999
The pattern class, which is a private type, cannot be created by new and obtains a matching pattern through Pattern.compile (). The pattern class can perform simple matches, such as the segmentation of a string, and the entire string match.
The Matcher class that obtains the Matcher object by Pattern.matcher (string). Matcher.find () method, try to make the next match in the input string, M.start () and M.end () will change after each match.
Greedy/reluctant/possessive https://docs.oracle.com/javase/tutorial/essential/regex/quant.html
Greedy mode such as x? First try to match the entire string, and if no match is found, return the last character of the string and try to match again.
Reluctant mode such as x?? Start with the string header, and if no match is found, add one character at a time to re-match.
The possesive mode, such as x?+, tries to match the entire string and returns the result if no match is found.
Example:
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 sea Rch: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:xfooxxxxx Xfoono match found.
*,?, + the Difference
* and? can match 0 or more, there is a zero-length match, and + matches at least one
Enter your regex:a? Enter input string to Search:ai found the text "a" starting at index 0 and ending at index 1.I found the text "" Starting At index 1 and ending @ index 1.Enter your regex:a*enter input string to Search:ai found the text "a" starting at Inde x 0 and ending at index 1.I found the text "" Starting @ Index 1 and ending at index 1.Enter your regex:a+enter input St Ring to Search:ai found the text "a" starting at index 0 and ending at index 1.
and? The difference
In the following case, the pair is matched more than once, while * matches the longest one.
Enter your regex:a? Enter input string to Search:aaaaai found the text "a" starting at index 0 and ending at index 1.I found the text "a" STA Rting at index 1 and ending at index 2.I found the text "a" starting at index 2 and ending at index 3.I found the text "a" Starting at index 3 and ending at index 4.I found the text "a" starting at index 4 and ending at index 5.I found the text "" Starting at index 5 and ending at index 5.Enter your regex:a*enter input string to Search:aaaaai found the text "AAA AA "starting at index 0 and ending at index 5.I found the text" "Starting at index 5 and ending at index 5.Enter your reg Ex:a+enter input string to Search:aaaaai found the text "AAAAA" starting at index 0 and ending at index 5.
The way to take the real and imaginary parts of this leetcode:
Int[] Extractop (String complex) { Pattern p = pattern.compile ("([ -0-9]*) \\+ ([ -0-9]*) i"); Matcher m = p.matcher (complex); String tmp; int[] re = new int[2]; while (M.find ()) { tmp = M.group (1); if (Tmp.startswith ("-")) { re[0] =-(int) integer.valueof (tmp.substring (1,tmp.length ())); } else { re[0] = INTEGER.VALUEOF (TMP); } TMP = M.group (2); if (Tmp.startswith ("-")) { re[1] =-(int) integer.valueof (tmp.substring (1,tmp.length ())); } else { re[ 1] = integer.valueof (TMP); } } return re; }
4-30 Java Regular match