java正则表达式模式匹配_[ Java学习 ] 正则表达式与模式匹配

文章内容:

因为最近粗略了解学习了一下正则表达式,贴出我查阅过的,觉得比较好的正则表达式的文章链接,并且给出几段Java代码,可以用来测试自己学习掌握正则表达式的效果。

先贴出正则表达式的相关博文(都是超链接,可直接点击)

以及相关的Java代码:

import java.util.regex.*;

public class test{

public static void main(String args[ ]){

Pattern p;

Matcher m;

String s1="loveyouhatemelove123jkjhate999love888";

p=Pattern.compile("love\\w{3}|hate\\w{2}");

m=p.matcher(s1);

while(m.find()){

String str=m.group();

System.out.print("从"+m.start()+"到"+m.end()+"匹配模式子序列:");

System.out.println(str);

}

}

}

import java.util.regex.*;

public class test {

public static void main(String args[ ]){

Pattern p; //模式对象

Matcher m;//匹配对象

String input=

"Have 7 monkeys on the tree, walk 2 monkeys, still leave how many monkeys?";

p=Pattern.compile("monkeys"); //初始化模式对象

m=p.matcher(input); //初始化匹配对象

while(m.find()){

String str=m.group();

System.out.println("从"+m.start()+"至"+m.end()+"是"+str);

}

}

}

import java.util.regex.*;

public class test{

public static void main(String args[ ]){

Pattern p; //模式对象

Matcher m; //匹配对象

String s1="0A1A2A3A4A5A6A7A8A9"; //待匹配的字符序列

p=Pattern.compile("\\dA\\d"); //用模式"\\dA\\d"初始化模式对象

m=p.matcher(s1); //用待匹配字符序列初始化匹配对象

while(m.find()){

String str=m.group();

System.out.print("从"+m.start()+"到"+m.end()+"匹配模式子序列:");

System.out.println(str);

}

String temp=m.replaceAll("***");

System.out.println(temp);

System.out.println(s1);

m=p.matcher("9A00A3"); //重新初始化匹配对象

if(m.matches()){

String str=m.group();

System.out.println(str);

}

else{

System.out.println("不完全匹配");

}

if(m.lookingAt()){

String str=m.group();

System.out.println(str);

}

}

}

import java.util.regex.*;

public class test{

public static void main(String args[ ]){

Pattern p;

Matcher m;

String s="2008年08月08日20点,北京奥运开幕";

p=Pattern.compile("\\d+"); //查找s中的数字信息所用模式

m=p.matcher(s); //用待匹配字符序列初始化匹配对象

while(m.find()){

String str=m.group();

System.out.print("从"+m.start()+"到"+m.end()+"匹配模式子序列:");

System.out.println(str);

}

p=Pattern.compile("\\D+"); //查找s中的非数字信息所用模式

m=p.matcher(s);

while(m.find()){

String str=m.group();

System.out.print("从"+m.start()+"到"+m.end()+"匹配模式子序列:");

System.out.println(str);

}

}

}

import java.util.regex.*;

public class test {

public static void main(String args[ ]) {

Pattern p; //模式对象

Matcher m; //匹配对象

String regex = "(http://|www)\56?\\w+\56{1}\\w+\56{1}\\p{Alpha}+";

p = Pattern.compile(regex); //初始化模式对象

String s = "新浪:www.sina.cn,央视:http://www.cctv.com";

m = p.matcher(s); //得到检索s的匹配对象m

while(m.find()) {

String str = m.group();

System.out.println(str);

}

System.out.println("剔除网站地址后:");

String result = m.replaceAll("");

System.out.println(result);

}

}

-------------------------------其他相关文章------------------------------

[Java学习 ] 类的其他文章汇总(都是超链接,可直接点击):

-------------------------------一点碎碎念------------------------------

其实之前有过至少两次机会学习正则表达式的,但是都被我拖延过去了。

一次是今年暑假,当时刚上完大一所有课,在知乎上搜索,有什么适合学完C语言的人练手的小项目(大概是这个问题,具体记不清楚了),当时好像轮子哥就提到了正则表达式,然而并没有什么用,知道了我也还是没去学。

第二次是,某次和师兄请教问题时,他说到,正则表达式很有用,很值得一学,当时一听,这名词我听过啊!~赶紧回去就准备学一下….

然而懒癌太严重了,还是没有学。直到最近,老师的教案里出现了正则表达式了,好像这次是再也避不过了,就只能好好地理解了一下语法,算是对正则表达式有了粗浅的了解。

唉,现在突然觉得,有什么事情一定不能拖。不懂的知识点还是要在当时立刻解决,毕竟,现在不解决,早晚有一天也还是要还债的。拖着拖着,总有一天还是得花时间解决的/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值