使用split分割时:

String[] a="aa|bb|cc".split("|");

output:
[a, a, |, b, b, |, c, c]

先看一下split的用法:

 String[] java.lang.String.split(String regex)

Splits this string around matches of the given regular expression. 

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array. 

The string "boo:and:foo", for example, yields the following results with these expressions: 

Regex Result
: { "boo", "and", "foo" }}
o { "b", "", ":and:f" }} Parameters:
regex the delimiting regular expression
Returns:
the array of strings computed by splitting this string around matches of the given regular expression
Throws:
PatternSyntaxException - if the regular expression's syntax is invalid
Since:
1.4
See Also:
java.util.regex.Pattern
@spec
JSR-51

可以看到split中参数是一个正则表达式,正则表达式中有一些特殊字符需要注意,它们有自己的用法:

http://www.fon.hum.uva.nl/praat/manual/Regular_expressions_1__Special_characters.html

The following characters are the meta characters that give special meaning to the regular expression search syntax:

\ the backslash escape character.
The backslash gives special meaning to the character following it. For example, the combination "\n" stands for the newline, one of the control characters. The combination "\w" stands for a "word" character, one of the convenience escape sequences while "\1" is one of the substitution special characters.
Example: The regex "aa\n" tries to match two consecutive "a"s at the end of a line, inclusive the newline character itself.
Example: "a\+" matches "a+" and not a series of one or "a"s.
^ the caret is the start of line anchor or the negate symbol.
Example: "^a" matches "a" at the start of a line.
Example: "[^0-9]" matches any non digit.
$ the dollar is the end of line anchor.
Example: "b$" matches a "b" at the end of a line.
Example: "^b$" matches the empty line.
{ } the open and close curly bracket are used as range quantifiers.
Example: "a{2,3}" matches "aa" or "aaa".
[ ] the open and close square bracket define a character class to match a single character.
The "^" as the first character following the "[" negates and the match is for the characters not listed. The "-" denotes a range of characters. Inside a "[ ]" character class construction most special characters are interpreted as ordinary characters.
Example: "[d-f]" is the same as "[def]" and matches "d", "e" or "f".
Example: "[a-z]" matches any lowercase characters in the alfabet.
Example: "[^0-9]" matches any character that is not a digit.
Example: A search for "[][()?<>.*?]" in the string "[]()?<>.*?" followed by a replace string "r" has the result "rrrrrrrrrrrrr". Here the search string is one character class and all the meta characters are interpreted as ordinary characters without the need to escape them.
( ) the open and close parenthesis are used for grouping characters (or other regex).
The groups can be referenced in both the search and the substitution phase. There also exist some special constructs with parenthesis.
Example: "(ab)\1" matches "abab".
. the dot matches any character except the newline.
Example: ".a" matches two consecutive characters where the last one is "a".
Example: ".*\.txt$" matches all strings that end in ".txt".
* the star is the match-zero-or-more quantifier.
Example: "^.*$" matches an entire line.
+ the plus is the match-one-or-more quantifier.
? the question mark is the match-zero-or-one quantifier. The question mark is also used in special constructs with parenthesis and in changing match behaviour.
| the vertical pipe separates a series of alternatives.
Example: "(a|b|c)a" matches "aa" or "ba" or "ca".
< > the smaller and greater signs are anchors that specify a left or right word boundary.
- the minus indicates a range in a character class (when it is not at the first position after the "[" opening bracket or the last position before the "]" closing bracket.
Example: "[A-Z]" matches any uppercase character.
Example: "[A-Z-]" or "[-A-Z]" match any uppercase character or "-".
& the and is the "substitute complete match" symbol.

那么上述方法的解决方法是使用转义来分割:

String[] a="aa|bb|cc".split("\\|");

小结:

对字符串的正则操作时要注意特殊字符的转义。

使用split进行分割时遇到特殊字符的问题的更多相关文章

  1. Java字符串split分割星号*等特殊字符问题(转)

    Java的split()方法分割字符串比较常用(见[Java]字符串以某特殊字符分割处理 ),但在有的时候,会遇到星号*等正则表达式中的特殊字符而无法分割的问题. 比如某需求,用户输入产品规格:厚*宽 ...

  2. 通过split命令分割大文件

    场景 线上出了问题,我需要去查找log来定位问题,但是由于线上数据量庞大,这些log文件每过一个小时就会自动回滚一次,尽管如此,有的log文件依然达到了五六g以上的大小. 对于这种巨大的log文件,常 ...

  3. javascript中split字符串分割函数

    1. var ss=s.split("fs"); for(var i=0;i<ss.length;i++){ 处理每一个ss[i]; } 2. "2:3:4:5&q ...

  4. Split字符串分割函数

    非常非常常用的一个函数Split字符串分割函数. Dim myTest myTest = "aaa/bbb/ccc/ddd/eee/fff/ggg" Dim arrTest arr ...

  5. C#实现字符串按多个字符采用Split方法分割

    原文:C#实现字符串按多个字符采用Split方法分割 String字符串如何按多个字符采用Split方法进行分割呢?本文提供VS2005和VS2003的实现方法,VS2005可以用下面的方法: str ...

  6. linux下使用split 来分割大文件

    linux下使用split 来分割大文件 2010-07-27 15:46:27|  分类: 技术文稿 |  标签:split  分割  linux   |字号 订阅   平常都是使用ssh来进行远程 ...

  7. freemarker中的split字符串分割

    freemarker中的split字符串分割 1.简易说明 split分割:用来根据另外一个字符串的出现将原字符串分割成字符串序列 2.举例说明 <#--freemarker中的split字符串 ...

  8. freemarker中的split字符串分割(十六)

    1.简易说明 split分割:用来根据另外一个字符串的出现将原字符串分割成字符串序列 2.举例说明 <#--freemarker中的split字符串分割--> <#list &quo ...

  9. javascript 中 split 函数分割字符串成数组

    分割字符串成数组的方法有很多,不过使用最多的还是split函数 <script language="javascript"> str="2,2,3,5,6,6 ...

随机推荐

  1. 前端-SEO

    SEO是 search Engine Optimization   (搜索引擎优化) SEO: ①白帽SEO(普通SEO做的优化) 网站标题.关键字.描述 网站内容优化 Robot.txt文件 网站地 ...

  2. 【简易版】Java ArrayList(增删改查)

    1.什么是ArrayList ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处: (1)动态的增加和减少元素 (2)实现了ICollectio ...

  3. IE6下解决select层级高的问题

    div在IE6下无法遮盖select,原因是在IE6下,浏览器将select元素视为窗口级元素,这时div或者其它的普通元素无论z-index设置的多高都是无法遮住select元素的. 解决方法有三种 ...

  4. svo的一些博客解析

    记录一边学习 白巧克力: svo代码笔记 http://blog.csdn.net/heyijia0327/article/details/51083398 卢彦斌:svo原理解析 东北大学孙志明:s ...

  5. 用Pyton玩转数据练习题---第二周

    找前5个默尼森数.P是素数且M也是素数,并且满足等式M=2**P-1,则称M为默尼森数.例如,P=5,M=2**P-1=31,5和31都是素数,因此31是默尼森数. # coding:utf-8 im ...

  6. use zlib lib to compress or decompress file

    If you want to compress or decompress file when writing C++ code,you can choose zlib library,that's ...

  7. React Native图片缓存解决方案

    1. react-native-fetch-blob 将图片存在本地的一个东西 2. react-native-img-cache 自动缓存的一个东西 上面装好后 就可以使用啦 import {Cac ...

  8. zabbix对数据盘磁盘容量进行监控

    示例将数据盘挂载到 /mnt目录 , 对 /mnt目录进程容量监控 item 添加对 /mnt 目录的监控项 tragger 添加触发项 这样完成对一个数据盘磁盘容量的监控

  9. 005-hive概述,计算原理及模型

    计算原理及模型 优化的根本思想: 尽早尽量过滤数据,减少每个阶段的数据量 减少job数 解决数据倾斜问题 Hive概述 名称       hive系统架构 metastore derbymysql   ...

  10. 20145314郑凯杰 《Java程序设计》第3周学习总结

    20145314郑凯杰 <Java程序设计>第3周学习总结 所有代码均已托管 地址https://git.oschina.net/qiaokeli26/codes 按照下面程序结果中的代码 ...