使用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("\\|");

小结:

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

转载于:https://www.cnblogs.com/davidwang456/p/4264473.html

使用split进行分割时遇到特殊字符的问题相关推荐

  1. java String.split()部分分割符无法识别,需要进行转义

    之前使用String.split()方法进行字符串分割时,发现使用一些特殊的分隔符时,分割后的字符串数组有问题,经查是部分分隔符需要转义,特此记录 /*** 检测 分隔符 是否存在特殊字符* @par ...

  2. linux中split分割文件打开方式,Linux系统下使用split命令分割大文件 (转载)

    [小蜗牛闲情之作 ] 我想给一个朋友传一个大视频,有几百M,尝试多种传输办法失败后,最后想到的是把视频切开一片片"邮递"过去给他,让它自己组装起来吧. [root@pps publ ...

  3. python字符串二(find();index();count();rfind();rindex();replace();替换;.split();分割;join();合并)

    1.下标查找所需单个字符 若输出第一,二个字符 word='xlccc' print(word[0]) print(word[1]) 即字符串编号从0开始 2.切片,截取字符串一段字符 语法:序列[开 ...

  4. 字符串以.作为split()的分割符

    当以.(点号)作为String.split()的分割符时,表达式不应该写成String.split("."),因为点号在正则表达式中由特殊含义,所以此处应该用转义字符String. ...

  5. 站长在线Python精讲:在Python中使用split()方法分割、使用join()方法合并字符串详解

    欢迎你来到站长在线的站长学堂学习Python知识,本文学习的是<在Python中使用split()方法分割.使用join()方法合并字符串详解>.本知识点主要内容有:在Python中使用s ...

  6. js根据字符串时分秒获取总秒数和分割时分秒

    起初是因为一些网站的视频没找着时间戳,但是显示了视频的时长时分秒,为了做过滤处理,就想到了获取到视频上显示的的时分秒,进行截取,然后根据秒数判断该视频的时长进行筛选 /*** 分割时分秒字符串* @p ...

  7. linux文件分割命令性能,Linux系统下使用split命令分割大文件 (转载)

    [小蜗牛闲情之作 ] 我想给一个朋友传一个大视频,有几百M,尝试多种传输办法失败后,最后想到的是把视频切开一片片"邮递"过去给他,让它自己组装起来吧. [root@pps publ ...

  8. Split字符串分割函数

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

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

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

最新文章

  1. 单片机检测stc没反应_我为什么不用STC单片机
  2. 分别用Comparable和Comparator两个接口对下列四位同学的成绩做降序排序,如果成绩一样, 那在成绩排序的基础上按照年龄由小到大排序。 姓名(String
  3. pythonopencv目标检测_Python 使用Opencv实现目标检测与识别的示例代码
  4. CentOS6 修改MySQL编码
  5. C#集成FastDFS断点续传
  6. 1036: 谭浩强C语言(第三版)习题1.6
  7. bp神经网络java代码_BP神经网络的Java实现
  8. myeclipse登陆问题
  9. html默认office打开如何更改,怎么设置office默认打开方式,修改office的默认打开方式...
  10. 联想笔记本G50-80 bios白名单修改
  11. cocos2d_lua 2.5D
  12. 用U深度启动U盘清除系统登入密码
  13. Excel下的数据挖掘:学生成绩统计分析实战之前言
  14. 码元 码元传输速率 波特率 比特率 数据率
  15. GDAL读取Jpeg2000格式图像
  16. 超值干货 | 建议收藏:精美详尽的 HTTPS 原理图注意查收!
  17. python统计代码行数_python实现统计代码行数的方法
  18. 网页 SEO 优化(搜索引擎优化)
  19. OpenCV函数应用:基于二值图像的三种孔洞填充方法记录(附python,C++代码)
  20. 用nginx搭建http透明代理

热门文章

  1. ug怎么画曲线_UG怎么画雨伞的曲面造型
  2. c++自底向上算符优先分析_c语言运算符的优先级
  3. 弹性服务器怎么上传文件,上传哪个文件夹弹性云服务器
  4. cron表达式 每天0点10分和30分_“提前2小时下班”:学会这10个函数,办公效率提升了10倍!...
  5. c语言 int64 t占位符,为什么我会得到“您必须为dtype int64提供占位符张量输出值”?...
  6. mysql主从复制原理 简书_mysql主从复制,从原理讲到安装配置,全干货
  7. STM32外设之GPIO的推挽输出和开漏输出模式详解
  8. stm32硬件设计手册_基于STM32自制三菱PLC之软硬件设计
  9. 计算机英语发展历史,英语翻译计算机发展史,领域与未来发展 一、计算机发展史简介 人类所使用的计算工具是随着生产的发展和社会的进步,从简单到复...
  10. conda install pytorch torchvision cudatoolkit=10.2 -c pytorch 报错