JAVA正则表达式
来源:互联网 发布:汉字笔顺演示软件 编辑:程序博客网 时间:2024/06/18 12:44
1. 一般来说,正则表达式就是以某种方式来描述字符串。
在其他语言中,\\表示“我想要在正则表达式中插入一个普通的(字面上的)反斜线,请不要给它任何特殊的意义。”而在Java中,\\的意思是“我要插入一个正则表达式的反斜线,所以其后的字符具有特殊的意义。”例如,如果你想表示一位数字,那么正则表达式应该是\\d。如果你想插入一个普通的反斜线,则应该这样\\\\。不过换行和制表符之类的东西只需使用单反斜线:\n\t。
?表示可能有某个字符。如-?表示可能有一个负号在前面。
+表示一个或多个之前的表达式。
2. String类自带正则表达式工具:
1)matches , 检查string是否匹配正则表达式
"-1234".matches("-?\\d+"); //true2) split , 将字符串从正则表达式匹配的地方切开(匹配的部分被删除)
"you must do it".split("\\W+");//you, must, do, it
3) replaceFirst ,replaceAll , 替换
"you found it".replaceFirst("f\\w+","located"); //"you located it""you found it".replaceAll("f\\w+","located"); //"you located it"3. 创建正则表达式(导入java.util.regex)
在JDK文档的java.util.regex.Pattern那一页有完整的下述表达式。
Construct Matches Characters xThe character x\\The backslash character\0nThe character with octal value 0n (0 <= n <= 7)\0nnThe character with octal value 0nn (0 <= n <= 7)\0mnnThe character with octal value 0mnn (0 <= m <= 3, 0 <= n <= 7)\xhhThe character with hexadecimal value 0xhh\uhhhhThe character with hexadecimal value 0xhhhh\x{h...h}The character with hexadecimal value 0xh...h (Character.MIN_CODE_POINT
<= 0xh...h <=Character.MAX_CODE_POINT
)\tThe tab character ('\u0009')\nThe newline (line feed) character ('\u000A')\rThe carriage-return character ('\u000D')\fThe form-feed character ('\u000C')\aThe alert (bell) character ('\u0007')\eThe escape character ('\u001B')\cxThe control character corresponding to xCharacter classes [abc]
a
,b
, orc
(simple class)[^abc]
Any character excepta
,b
, orc
(negation)[a-zA-Z]
a
throughz
orA
throughZ
, inclusive (range)[a-d[m-p]]
a
throughd
, orm
throughp
:[a-dm-p]
(union)[a-z&&[def]]
d
,e
, orf
(intersection)[a-z&&[^bc]]
a
throughz
, except forb
andc
:[ad-z]
(subtraction)[a-z&&[^m-p]]
a
throughz
, and notm
throughp
:[a-lq-z]
(subtraction)Predefined character classes .Any character (may or may not match line terminators)\dA digit: [0-9]\DA non-digit: [^0-9]\hA horizontal whitespace character: [ \t\xA0\u1680\u180e\u2000-\u200a\u202f\u205f\u3000]\HA non-horizontal whitespace character: [^\h]\sA whitespace character: [ \t\n\x0B\f\r]\SA non-whitespace character: [^\s]\vA vertical whitespace character: [\n\x0B\f\r\x85\u2028\u2029]\VA non-vertical whitespace character: [^\v]\wA word character: [a-zA-Z_0-9]\WA non-word character: [^\w]
4. 量词
量词描述了一个模式吸收输入文本的方式:
1)贪婪型:为所有可能的模式发现尽可能多的匹配。
2)勉强型:用问号来指定,匹配满足模式所需的最少字符数
3)占有型。
5. CharSequence
接口CharSequence从CharBuffer、String、StringBuffer、StringBuilder类之中抽象出了字符序列的一般化定义:
interface CharSequence { charAt(int i); length(); subSequence(int start, int end); toString();}6. Pattern和Matcher
Pattern p=Pattern.compile("abc+");Matcher m=p.matcher("abcabcac");while(m.find){ println("Match \""+m.group()+"\" at positions "+m.start()+"-"+(m.end()-1)); //Match "abc" at positions 0-2 //Match "abc" at positions 3-5}Matcher还有matches,
lookingAt(判断字符串的始部分能否匹配模式),
start(返回先前匹配的起始位置的索引),
end返回所匹配的最后字符的索引加一的值,
split,replaceFirst,replaceAll,
appendReplacement进行渐进式的替换(即对每一匹配的字符串进行逐个的替换,例如对匹配的元音字母转换成对应的大写形式),
appendTail(StringBuffer sbuf)在执行了一次或多次appendReplacement之后调用此方法可以将输入字符串余下的部分复制到sbuf中,
reset可以将现有的Matcher对象应用于一个新的字符序列。
0 0
- Java 正则表达式正则
- 【正则表达式】Java正则表达式
- 正则表达式 JAVA正则表达式
- 【Java】【正则表达式】正则表达式
- Java 正则表达式详解_正则表达式
- java正则表达式!正则表达式踩坑!!!
- 正则表达式工具类,正则表达式封装,Java正则表达式
- Java正则表达式详解
- Java正则表达式详解
- Java正则表达式详解
- Java正则表达式详解
- Java正则表达式详解
- java和正则表达式
- java正则表达式详解!
- Java正则表达式详解
- Java正则表达式详解
- Java正则表达式详解
- Java正则表达式详解
- USTC 1280 / 携程决赛1004 最短路径的代价
- POJ 1459 Power Network
- UVa941 - Permutations
- TYVJ 1049 最长不下降子序列
- matlab中的gallery函数简析
- JAVA正则表达式
- 寻找矩阵中的鞍点
- 一些常用的样式
- 程序员:伤不起的三十岁
- 【大话QT之四】ctkPlugin插件系统实现项目插件式开发
- ZOJ 2107
- 4-oracle-启动流程分析
- 查看数据库名 查看实例名 服务名
- ios安全攻防(五)使用Cycript修改支付宝app运行时