Linux学习:正则表达式习题
2015-09-08 10:12
363 查看
一直一来遇到"替换为/" 的时候概念总是很模糊,今天要搞搞清楚。
需要注意的是:/在正则表达式里需要再转义一次,所以replaceAll的第二个参数变得很长。 但是最后一行为什么也是可以的,这个我就不明白了,查了javadoc(在james的引导下),发现有这样一段话: It is an error to use a backslash prior to any alphabetic character that does not denote an escaped construct; these are reserved for future extensions to the regular-expression language. A backslash may be used prior to a non-alphabetic character regardless of whether that character is part of an unescaped construct. 此外还有,这样/这个符号在正则中如果什么都没有跟(没有转义),那么只是简单的引用后面那个字符; 配合上面那段文档也就是说,/+非字母字符 就等于 这个非字母字符(ie:/= 和 = 是一样),这就解释了 /"和"是一样的原因,哈哈。 搞了半天是javadoc没看仔细啊~。。。
System.out.println("/"".replace("/"", "///"")); System.out.println("/"".replaceAll("/"", "/////"")); System.out.println("/"".replaceAll("///"", "/////""));
需要注意的是:/在正则表达式里需要再转义一次,所以replaceAll的第二个参数变得很长。 但是最后一行为什么也是可以的,这个我就不明白了,查了javadoc(在james的引导下),发现有这样一段话: It is an error to use a backslash prior to any alphabetic character that does not denote an escaped construct; these are reserved for future extensions to the regular-expression language. A backslash may be used prior to a non-alphabetic character regardless of whether that character is part of an unescaped construct. 此外还有,这样/这个符号在正则中如果什么都没有跟(没有转义),那么只是简单的引用后面那个字符; 配合上面那段文档也就是说,/+非字母字符 就等于 这个非字母字符(ie:/= 和 = 是一样),这就解释了 /"和"是一样的原因,哈哈。 搞了半天是javadoc没看仔细啊~。。。
相关文章推荐
- vi 删除内容操作
- Linux命令ps,netstat,top,time,cat,uname
- Linux快捷键和别名
- Linux下VIM作为IDE:玩转cscope+ctags
- Ubuntu14.04下SP_Flash_Tool_exe_Linux无法烧录
- Linux学习:正则表达式的运用
- centos6的 shell基础学习
- arm-linux-gdb、gdbserver的编译安装
- 新手指南: Linux 新手应该知道的 26 个命令
- 熟悉linux-安装Maven
- Linux Tips: 修改启动过程获得root权限
- Centos 6.4 python 2.6.6 升级到 2.7.3
- 每天一个linux命令(8):crontab命令
- 每天一个linux命令(7):at命令
- Linux用户和组的基本概念和配置
- window程序移植到linux下(一)
- Linux下搭建FTP服务器
- linux 系统下查找多个文件包含特定关键字 查询
- UNIX/Linux 任务计划at和crontab使用详解
- linux开机自动启动