Note the following when using split (regex) in java: Regular Expression

Source: Internet
Author: User

For example (,),., |, * and other symbols of the class: String area = "(30.13206313822174, 120.4156494140625) (29.87637380707133, 120.1629638671875) (29.882327547852515, 120.50079345703125)"; int t = area. split ("\\)"). length; System. out. println (t); public string [] split (string regex) the parameter name here is regex, that is, regular expression (regular expression ). This parameter is not a simple delimiter, but a regular expression. After reading the implementation code of the split method, we are more confident: public string [] split (string regex, int limit) {return pattern. compile (regex ). split (this, limit);} The split method of the matcher class directly called by the split implementation. Readers already know that "." has special meanings in regular expressions, so we must escape it when using it. Public static void main (string [] args) {string value = "192.168.128.33"; // you must add \ or disable it. yeahstring [] names = value. split ("\\. "); for (int I = 0; I <names. length; I ++) {system. out. println (names [I]);} output: 19216812833 in JAVA, using String. when using the split method to separate strings, note that some characters cannot be used directly. If some special characters such as "|", "*" are used as separators, otherwise problems may occur, I used to read the code for half a day because of this problem. Public String [] split (String regex) Splits this string around matches of the given regular expression. the regex parameter is a regular-expression matching mode instead of a simple String. It may produce unexpected results for some special characters, for example, test the following code: use vertical bars | to separate strings. You will not get the expected results String [] aa = "aaa | bbb | ccc ". split ("|"); // String [] aa = "aaa | bbb | ccc ". split ("\ |"); in this way, you can get the correct result for (int I = 0; I <aa. length; I ++) {System. out. println ("--" + aa [I]);} running with vertical * Separator will throw java. u Til. regex. PatternSyntaxException exception. This is also true with the plus sign +. String [] aa = "aaa * bbb * ccc ". split ("*"); // String [] aa = "aaa | bbb | ccc ". split ("\ *"); to obtain the correct result for (int I = 0; I <aa. length; I ++) {System. out. println ("--" + aa [I]);} Obviously, + * is not a valid Regular Expression for pattern matching, escape with "\ *" "\ +" to get the correct result. "|" The separator string can be executed, but it is not the expected purpose. "\ |" escape to get the correct result. If you want to use the "\" character in the string, you also need to escape it. the expression "aaaa \ bbbb" should be "aaaa \ bbbb" first. If it is to be separated, the correct result should be obtained: string [] aa = "aaa \ bbb \ bccc ". split (\\\\);

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.