日期:2014-05-20 浏览次数:20591 次
"
hello:aaaaaaa \n
world:bbbbbbb \n
ni:ccccccc \n
hao:ddddddd \n
"
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringRex
{
public static void main(String[] args)
{
String str = "hello:aaaaaaa \n world:bbbbbbb \n ni:ccccccc \n hao:ddddddd \n";
//第一位是a到z出现一次或多次,第二位是":",第三位是a-z出现一次或多次
//\\s+代表空白字符,包括Tab建
//\\\n 就等同于\n 前两个斜杠是转义字符
String rex = "[a-z]+:[a-z]+\\s+\\\n";
Pattern p = Pattern.compile(rex);
Matcher m = p.matcher(str);
while(m.find())
{
String name = m.group();
if(name.contains("world"))
{
String delName = name;
System.out.println("已删除的字符串:"+delName);
continue;
}
System.out.println(name);
}
}
}