当前页面: 开发资料首页 → Java 专题 → 在JAVA中使用正则表达式
在JAVA中使用正则表达式
摘要: 在JAVA中使用正则表达式
</td>
</tr>
<tr>
<td height="35" valign="top" class="ArticleTeitle">
<table width="734" border="0">
<tr>
<td width="728" height="108">
</td>
</tr>
</table> ?
jdk1.4中加入了java.util.regex包提供对正则表达式的支持。而且Java.lang.String类中的replaceAll和split函数也是调用的正则表达式来实现的。
正则表达式对字符串的操作主要包括:字符串匹配,指定字符串替换,指定字符串查找和字符串分割。下面就用一个例子来说明这些操作是如何实现的:
<%@ page import="java.util.regex.*"%>
<%
Pattern p=null; //正则表达式
Matcher m=null; //操作的字符串
boolean b;
String s=null;
StringBuffer sb=null;
int i=0;
//字符串匹配,这是不符合的
p = Pattern.compile("a*b");
m = p.matcher("baaaaab");
b = m.matches();
out.println("字符串\"baaaaab\"是否匹配正则表达式\"a*b\": "+b+"
");
//字符串匹配,这是符合的
p = Pattern.compile("a*b");
m = p.matcher("aaaaab");
b = m.matches();
out.println("字符串\"aaaaab\"是否匹配正则表达式\"a*b\": "+b+"
");
//字符串替换
p = Pattern.compile("ab");
m = p.matcher("aaaaab");
s = m.replaceAll("d");
out.println(s+"
");
p = Pattern.compile("a*b");
m = p.matcher("aaaaab");
s = m.replaceAll("d");
out.println(s+"
");
p = Pattern.compile("a*b");
m = p.matcher("caaaaab");
s = m.replaceAll("d");
out.println(s+"
");
//字符串查找并替换
p = Pattern.compile("cat");
m = p.matcher("one cat two cats in the yard");
sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "dog");
i++;
}
m.appendTail(sb);
out.println(sb.toString()+"
");
out.println(i+"
");
i=0;
p = Pattern.compile("cat");
m = p.matcher("one cat two ca tsi nthe yard");
sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "dog");
i++;
}
m.appendTail(sb);
out.println(sb.toString()+"
");
out.println(i+"
");
p = Pattern.compile("cat");
m = p.matcher("one cat two cats in the yard");
p=m.pattern();
m = p.matcher("bacatab");
b = m.matches();
out.println(b+"
");
s = m.replaceAll("dog");
out.println(s+"
");
i=0;
p = Pattern.compile("(fds){2,}");
m = p.matcher("dsa da fdsfds aaafdsafds aaf");
sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "dog");
i++;
}
m.appendTail(sb);
out.println(sb.toString()+"
");
out.println(i+"
");
//将cat加红色显示
p = Pattern.compile("cat");
m = p.matcher("one cat two cats in the yard");
sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "
cat");
}
m.appendTail(sb);
out.println(sb.toString()+"
");
String aa=sb.toString();
out.println(aa+"
");
//字符串分割
p = Pattern.compile("a+");
String[] a=p.split("caaaaaat");
for(i=0;i
{
out.println(a[i]+"
");
}
p = Pattern.compile("a+");
a=p.split("c aa aaaa t",0);
for(i=0;i
{
out.println(a[i]+"
");
}
//以空格分隔
p = Pattern.compile(" +");
a=p.split("c aa aaaa t",0);
for(i=0;i
{
out.println(a[i]+"
");
}
//以加号分隔
p = Pattern.compile("\\+");
a=p.split("dsafasdfdsafsda+dsagfasdfa+sdafds");
out.println(a.length+"
");
for(i=0;i
{
out.println(a[i]+"
");
}
%>
function TempSave(ElementID)
{
CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
CommentsPersistDiv.save("CommentXMLStore");
}
function Restore(ElementID)
{
CommentsPersistDiv.load("CommentXMLStore");
document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
}
</td>
</tr>
<tr>
↑返回目录
前一篇: 用JAVA程序取得IP地址
后一篇: 正则表达式的基本语法