java正则表达式简单使用

来源:互联网 发布:java虚拟机教程 编辑:程序博客网 时间:2024/05/27 16:42
import java.util.regex.*;public class test {    public static void main(String[]args)    {        分割字符串        Pattern pattern=Pattern.compile("[,|]+");        String[] str=pattern.split("Java hello world Java,hello,x,world|sun");        for(int i=0;i<str.length;i++){            System.out.println(str[i]);        //匹配邮箱        String str="xiaochen@qq.com.cn";        Pattern pattern=Pattern.compile("[\\w\\.]+@([\\w]+\\.)+[\\w]+",          Pattern.CASE_INSENSITIVE);        Matcher matcher=pattern.matcher(str);        System.out.println(matcher.matches());        //去除html标记        Pattern pattern=Pattern.compile("<.+?>",Pattern.DOTALL);        Matcher matcher=pattern.matcher("<a href=\"index.html\">baidu</a>");        String str=matcher.replaceAll("");        System.out.println(str);//baidu        //查找html中对应条件的字符串        Pattern pattern=Pattern.compile("href=\"(.+?)\"");        Matcher matcher=pattern.matcher("<a href=\"index.html\">baidu</a>");        if(matcher.find()){        System.out.println(matcher.group(1));}//index.html        }        //截取http地址        Pattern pattern=Pattern.compile("(http://|https://){1}[\\w\\.\\-/:]+");        Matcher matcher=pattern.matcher("dsdsds<http://dsds//gfgffdfd>fdf");        StringBuffer buffer=new StringBuffer();        while(matcher.find())            buffer.append(matcher.group());            buffer.append("\r\n");        System.out.println(buffer.toString());    }}
原创粉丝点击