javasplit正则_Java中正则表达式split()特殊符号使⽤详解特殊情况有 * ^ : | . \
⼀、单个符号作为分隔符
String address="上海\上海市|闵⾏区\吴中路";
String[] splitAddress=address.split("\\");
System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
String address="上海|上海市|闵⾏区|吴中路";
String[] splitAddress=address.split("\\|"); //如果以竖线为分隔符,则split的时候需要加上两个斜杠【\\】进⾏转义
System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
String address="上海*上海市*闵⾏区*吴中路";
String[] splitAddress=address.split("\\*");
System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
String address="上海:上海市:闵⾏区:吴中路";
String[] splitAddress=address.split("\\:");
System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
String address="上海.上海市.闵⾏区.吴中路";
String[] splitAddress=address.split("\\.");
System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
String address="上海^上海市^闵⾏区^吴中路";
String[] splitAddress=address.split("\\^");
System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
String address="上海@上海市@闵⾏区@吴中路";
String[] splitAddress=address.split("@");
System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
String address="上海,上海市,闵⾏区,吴中路";
String[] splitAddress=address.split(",");
System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
⼆、多个符号作为分隔符
String address="上海^上海市@闵⾏区#吴中路";
String[] splitAddress=address.split("\\^|@|#");
System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
例:
String address = new String("192.168.13.240");
String[] str = address.split("\\.");
java中split的用法
for(String s : str){
System.out.println(s);
}
输出格式:
192
168
13
240
总结:
(1)split表达式,其实就是⼀个正则表达式。* ^ | 等符号在正则表达式中属于⼀种有特殊含义的字符,如果使⽤此种字符作为分隔符,必须使⽤转义符即\\加以转义。
(2)如果使⽤多个分隔符则需要借助 | 符号,如⼆所⽰,但需要转义符的仍然要加上分隔符进⾏处理
到此这篇关于Java中正则表达式split()特殊符号使⽤详解的⽂章就介绍到这了,更多相关Java split()内容请搜索脚本之家以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持脚本之家!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论