1、简单认识正则表达式的概念
print("abc".matches("..."));//true
print("a8729a".replaceAll("\\d", "-"));//a----a
Pattern p = Pattern.compile("[a-z]{3}");
Matcher m = p.matcher("fgh");
print(m.matches());//true
print("fgha".matches("[a-z]{3}"));//false
2、初步认识. * + ? {n,m}
print("a".matches("."));//true
print("aa".matches("aa"));//true
print("aaaa".matches("a*"));//true 0个或多个
print("aaaa".matches("a+"));//true 1个或多个
print("".matches("a*"));// true
print("aaaa".matches("a?"));//false 0个或1个
print("".matches("a?")); //true
print("a".matches("a?"));//true
print("214523145234532".matches("\\d{3,100}"));//true at least 3 but not more than 100 times
print("192.168.0.aaa".matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"));//false
print("192".matches("[0-2][0-9][0-9]"));//true
3、范围
print("a".matches("[abc]"));//true
print("a".matches("[^abc]"));//false
print("A".matches("[a-zA-Z]"));//true
print("A".matches("[a-z]|[A-Z]"));//true
print("A".matches("[a-z[A-Z]]"));//true
print("R".matches("[A-Z&&[RFG]]"));//true
4、认识\s \w \d \S \W \D
print(" \n\r\t".matches("\\s{4}"));//true
print(" ".matches("\\S"));//false
print("a_8".matches("\\w{3}"));//w is a word character [a-zA-Z_0-9] true
print("abc888&^%".matches("[a-z]{1,3}\\d+[&^#%]+"));//true
print("\\".matches("\\\\"));//true
5、boundary and whitelines
print("hello sir".matches("^h.*")); //true
print("hello sir".matches(".*ir$"));//true
print("hello sir".matches("^h[a-z]{1,3}o\\b.*"));//true
print("hellosir".matches("^h[a-z]{1,3}o\\b.*"));//false
print(" \n".matches("^[\\s&&[^\\n]]*\\n$"));//true
print("aaa 8888c".matches(".*\\d{4}.")); //true
print("aaa 8888c".matches(".*\\b\\d{4}.")); //true
print("aaa8888c".matches(".*\\d{4}."));//true
print("aaa8888c".matches(".*\\b\\d{4}."));//false
6、email
print("asdfasdfsafsf@dsdfsdf.com".matches("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+"));
7、matches find lookingAt
attern p = Pattern.compile("\\d{3,5}");
String s = "123-34345-234-00";
Matcher m = p.matcher(s);
print(m.matches());//false
m.reset();
print(m.find());//true
print(m.start() + "-" + m.end());//0-3
print(m.find());//true
print(m.start() + "-" + m.end());//4-9
print(m.find());//true
print(m.start() + "-" + m.end());//10-13
print(m.find());//false
print(m.lookingAt());//always from beggining true
print(m.lookingAt());true
print(m.lookingAt());true
print(m.lookingAt());true
8、replacement
Pattern p = Pattern.compile("java", Pattern.CASE_INSENSITIVE);//大小写不敏感
Matcher m = p.matcher("java Java JAVa JaVa IloveJAVA you hateJava afasdfasdf");
StringBuffer buf = new StringBuffer();
int i=0;
while(m.find()) {
i++;
if(i%2 == 0) {
m.appendReplacement(buf, "java");//第偶数个转换为小写
} else {
m.appendReplacement(buf, "JAVA");//第奇数个转换为大写
}
}
m.appendTail(buf);//把最后的字符串追加上
print(buf);
9、group
Pattern p = Pattern.compile("(\\d{3,5})([a-z]{2})");
String s = "123aa-34345bb-234cc-00";
Matcher m = p.matcher(s);
while(m.find()) {
print(m.group());//123aa 34345bb 234cc
}
分享到:
相关推荐
浅谈正则表达式在文本排版及校对中的应用 可以在word等中使用
浅谈正则表达式实例入门共9页.pdf.zip
很长时间没看 正则表达式了,碰巧今天用到,温故知新了一把 看书学习吧 50% 的举一反三练习中的原创。 一 javascript正则表达式的基本知识 1 javascript 正则对象创建 和用法 声明javascript 正则表达式 代码如下: ...
本文介绍了正则表达式的一些学习内容,以及在Javascript、PHP下如何使用正则表达式
问题描述:很多网站会对用户发帖内容进行一定的检查,并自动把敏感词修改为特定的字符。... 您可能感兴趣的文章:Python 实现王者荣耀中的敏感词过滤示例python 实现敏感词过滤的方法浅谈Python 敏感词过滤的实现
其实我对正则表达式的学习基本完全来源于犀牛书的第10章,真正看懂这一章,我觉得操作正则表达式应该不在话下。 我的答案 先给出我的答案吧: ‘abbccddd’.match(/(\w)\1*/g) // [“a”, “bb”, “cc”, “ddd”]...
正则表达式格式 PHP变成的利器,经过一段时间的学习,浅谈一下自己的心得!!!!
文档讲述了正则表达式的基本语法、使用技巧,是本人通过测试的学习总结,非常适合初学者,因为我也是作为初学者来研究的
一个正则表达式要如何书写才能同时匹配这两个数字呢?简单的字符表达式当然无法完成了,这个时候我们就可以定义一个字符集合(字符类)来进行匹配。这就是分组匹配了
下面小编就为大家带来一篇JavaScript正则表达式-非捕获性分组。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
比如我们要匹配以字母”a”开头字母”b”结尾的字符串,但是需要匹配的字符串在”a”后面含有很多个”b”,比如”a bbbbbbbbbbbbbbbbb”,那正则表达式是会匹配第一个”b”还是最后一个”b”呢?如果你使用了贪婪...
前言 Python文件默认的编码格式是ascii ,无法识别汉字,因为ascii码中没有中文。 所以py文件中要写中文字符时,一般在开头加 # -*- coding: utf-8 -*- 或者 #coding=utf-8。 这是指定一种编码格式,意味着用该编码...
grep 是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。通常grep有三种版本grep、egrep(等同于grep -E)和fgrep。egrep为扩展的grep,fgrep则为快速grep(固定的字符串来对文本进行...
下面小编就为大家带来一篇浅谈JS正则表达式的RegExp对象和括号的使用。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧