正则表达式的应用
当我们要处理一条长长的字符对象是,我们可以用Pattern类或者String内带的一些有用的正则表达式方法。 public class Test
{
public static void main(String[] args)
{
//要匹配的字符
String s="<div class="sTitle">可能存在类似的问题</div><div class="sFooter"><a class="sFirstNewAsk">我想提一个新问题</a></div>";
//匹配中文的正则表达式
String rege="[u4e00-u9fa5]+";
//获得HTML的标签和属性
System.out.println(Arrays.toString(s.split(rege)));
//替换第一段中文内容
System.out.println(s.replaceFirst(rege,"beyondboy"));
//替换所有的中文内容
System.out.println(s.replaceAll(rege,"scau"));
//只判断开头是否含有中文内容
System.out.println(s.matches(rege));
}
}
运行结果: public class Test
{
public static void main(String[] args)
{
//要匹配的字符
String s="58.27.82.161@02/10/2005n" +
"204.45.234.40@02/11/2005n" +
"58.27.82.161@02/11/2005n" +
"58.27.82.161@02/12/2005n" +
"58.27.82.161@02/12/2005n" +
"[Next log section with different data format]";
//匹配邮箱的正则表达式
String regex="(d+[.]){3}d+@(d{2}/){2}d+";
Pattern pattern=Pattern.compile(regex);
System.out.println(pattern.split(regex));
//获得该matcher对象
Matcher matcher=pattern.matcher(s);
while (matcher.find())
{
//获得与(d+[.]){3}d+@(d{2}/){2}d+匹配的内容
System.out.println("matcher.groud():"+matcher.group()+ "" at positions " +
matcher.start() + "-" + (matcher.end() - 1));
//获得与(d+[.])匹配的内容
System.out.println("matcher.groud(1):"+matcher.group(1));
//获得与(d{2}/)匹配的内容
System.out.println("matcher.groud(2):"+matcher.group(2));
}
//下面是比较matches()和lookingAt()区别
Matcher matcher2=test("beyondboy from scau","w+yond");
//lookingAt()只要前面第一部分匹配就会成功
System.out.println(matcher2.lookingAt());
//matches()只有整个字符内容匹配才能成功
System.out.println(matcher2.matches());
matcher2=test("beyondboy from scau",".*yond.*");
System.out.println(matcher2.lookingAt());
System.out.println(matcher2.matches());
//appendReplacement()和appendTail()方法的使用
Matcher matcher3=test("12345789123456987654","345");
StringBuffer buffer=new StringBuffer();
while (matcher3.find())
{
//添加匹配的前面部分的内容
matcher3.appendReplacement(buffer,"4");
System.out.println("matcher3.appendReplacement():"+buffer);
//添加没有匹配剩下的内容
matcher3.appendTail(buffer);
System.out.println("matcher3.appendTail():"+buffer);
}
buffer.setLength(0);
matcher3.reset();
//这两种方法一般是这样结合用的
while(matcher3.find())
matcher3.appendReplacement(buffer,"4");
matcher3.appendTail(buffer);
System.out.println("matcher3.appendTail():"+buffer);
}
private static Matcher test(String s,String regex)
{
Pattern pattern=Pattern.compile(regex);
Matcher matcher=pattern.matcher(s);
return matcher;
}
}
运行结果: public class Test
{
public static void main(String[] args)
{
//要匹配的字符
String s="<<<beyondboy>>>>> from >>scau ";
Scanner scanner=new Scanner(s);
scanner.useDelimiter("(<* *>* *)");
//获得英语字符
while(scanner.hasNext())
{
System.out.println(scanner.next());
}
//要匹配的字符
String threatData =
"58.27.82.161@02/10/2005n" +
"204.45.234.40@02/11/2005n" +
"58.27.82.161@02/11/2005n" +
"58.27.82.161@02/12/2005n" +
"58.27.82.161@02/12/2005n" +
"[Next log section with different data format]";
String pattern = "(d+[.]d+[.]d+[.]d+)@" +
"(d{2}/d{2}/d{4})";
Scanner scanner2=new Scanner(threatData);
while(scanner2.hasNext(pattern))
{
//获得匹配的内容
System.out.println(scanner2.next(pattern));
MatchResult match = scanner2.match();
String ip = match.group(1);
String date = match.group(2);
//分别输出(d{2}/d{2}/d{4}),(d+[.]d+[.]d+[.]d+)的内容
System.out.format("Threat on %s from %sn",date,ip);
}
}
}
运行结果: b e y o n d b o y f r o m s c a u 58.27.82.161@02/10/2005 Threat on 02/10/2005 from 58.27.82.161 204.45.234.40@02/11/2005 Threat on 02/11/2005 from 204.45.234.40 58.27.82.161@02/11/2005 Threat on 02/11/2005 from 58.27.82.161 58.27.82.161@02/12/2005 Threat on 02/12/2005 from 58.27.82.161 58.27.82.161@02/12/2005 Threat on 02/12/2005 from 58.27.82.161 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |