正则表达式初入门

前端之家收集整理的这篇文章主要介绍了正则表达式初入门前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
常用元字符
@H_301_4@ @H_301_4@
代码 说明
. 匹配除换行符以外的任意字符
\w 匹配字母或数字或下划线或汉字
\s 匹配任意的空白符
\d 匹配数字
\b 匹配单词的开始或结束
^ 匹配字符串的开始
$ 匹配字符串的结束

常用限定符
@H_301_4@ @H_301_4@
代码/语法 说明
* 重复零次或更多次
+ 重复一次或更多次
? 重复零次或一次
{n} 重复n次
{n,} 重复n次或更多次
{n,m} 重复n到m次

常用反义词
@H_301_4@ @H_301_4@
代码/语法 说明
\W 匹配任意不是字母,数字,下划线,汉字的字符
\S 匹配任意不是空白符的字符
\D 匹配任意非数字的字符
\B 匹配不是单词开头或结束的位置
[^x] 匹配除了x以外的任意字符
[^aeIoU] 匹配除了aeIoU这几个字母以外的任意字符



从java 代码来 一步步 解析


package src;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class test {

    public static void main(String[] args) {
        //简单认识正则表达式的概念
		/*
		p("abc".matches("..."));
		p("a8729a".replaceAll("\\d","-"));
		Pattern p = Pattern.compile("[a-z]{3}");
		Matcher m = p.matcher("fgh");
		p(m.matches());
		p("fgha".matches("[a-z]{3}"));
		*/

        //初步认识. * + ?
		/*
		p("a".matches("."));  //one
		p("aa".matches("aa"));
		p("aaaa".matches("a*"));  //zero or more
		p("aaaa".matches("a+"));   //one or more
		p("".matches("a*"));
		p("aaaa".matches("a?"));   //zero or best of one
		p("".matches("a?"));
		p("a".matches("a?"));
		p("214523145234532".matches("\\d{3,100}"));
		p("192.168.0.aaa".matches("\\d{1,3}\\.\\d{1,3}"));
		p("192".matches("[0-2][0-9][0-9]"));
		*/

        //范围
		/*
		p("a".matches("[abc]"));   //一个中括号里面匹配一个
		p("a".matches("[^abc]"));  //取除了abc 之外的其它
		p("A".matches("[a-zA-Z]")); //取小写的a-z 或者A-Z
		p("A".matches("[a-z]|[A-Z]")); //同上
		p("A".matches("[a-z[A-Z]]")); //同上
		p("R".matches("[A-Z&&[RFG]]"));    //A-Z 之中 并且  是【RFG】之中的3者之1
		*/

        //认识\s \w \d \

//		p(" \n\r\t".matches("\\s{4}"));
//		p(" ".matches("\\S"));  //正则表达式中2 个反斜杠才表示转义字符/,
//		p("a_8".matches("\\{3}"));
//		p("abc888&^%".matches("[a-z]{1,3}\\d+[&^#%]+"));
//		p("\\".matches("\\\\"));    //由于字符里面,一个反斜杠表示转移,要表示一个反斜杠 java 里面 需要\\ 个,而正则表达式里面,两个才表示一个反斜杠,所以是4个


        //POSIX Style
        //p("a".matches("\\p{Lower}"));

        //boundary
		/*
		p("hello sir".matches("^h.*"));
		p("hello sir".matches(".*ir$"));
		p("hello sir".matches("^h[a-z]{1,3}o\\b.*"));
		p("hellosir".matches("^h[a-z]{1,3}o\\b.*"));
		//whilte lines
		p(" \n".matches("^[\\s&&[^\\n]]*\\n$"));

		p("aaa 8888c".matches(".*\\d{4}."));
		p("aaa 8888c".matches(".*\\b\\d{4}."));
		p("aaa8888c".matches(".*\\d{4}."));
		p("aaa8888c".matches(".*\\b\\d{4}."));
		*/

        //email
        //p("asdfasdfsafsf@dsdfsdf.com".matches("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+"));

        //matches find lookingAt
		/*
		Pattern p = Pattern.compile("\\d{3,5}");
		String s = "123-34345-234-00";
		Matcher m = p.matcher(s);
		p(m.matches());
		m.reset();
		p(m.find());
		p(m.start() + "-" + m.end());
		p(m.find());
		p(m.start() + "-" + m.end());
		p(m.find());
		p(m.start() + "-" + m.end());
		p(m.find());
		//p(m.start() + "-" + m.end());
		p(m.lookingAt());
		p(m.lookingAt());
		p(m.lookingAt());
		p(m.lookingAt());
		*/

        //replacement
		/*
		Pattern p = Pattern.compile("java",Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher("java Java JAVa JaVa IloveJAVA you hateJava afasdfasdf");
		StringBuffer buf = new StringBuffer();
		int i=0;
		while(m.find()) {
			i++;
			if(i%2 == 0) {
				m.appendReplacement(buf,"java");
			} else {
				m.appendReplacement(buf,"JAVA");
			}
		}
		m.appendTail(buf);
		p(buf);
		*/

        //group
		/*
		Pattern p = Pattern.compile("(\\d{3,5})([a-z]{2})");
		String s = "123aa-34345bb-234cc-00";
		Matcher m = p.matcher(s);
		while(m.find()) {
			p(m.group());
		}
		*/

        //qulifiers
		/*
		Pattern p = Pattern.compile(".{3,10}+[0-9]");
		String s = "aaaa5bbbb68";
		Matcher m = p.matcher(s);
		if(m.find())
			p(m.start() + "-" + m.end());
		else
			p("not match!");
		*/

        //non-capturing groups
		/*
		Pattern p = Pattern.compile(".{3}(?=a)");
		String s = "444a66b";
		Matcher m = p.matcher(s);
		while(m.find()) {
			p(m.group());
		}
		*/

        //back refenrences
		/*
		Pattern p = Pattern.compile("(\\d(\\d))\\2");
		String s = "122";
		Matcher m = p.matcher(s);
		p(m.matches());
		*/

        //flags的简写
        //Pattern p = Pattern.compile("java",Pattern.CASE_INSENSITIVE);
//        p("Java".matches("(?i)(java)"));
    }

    public static void p(Object o) {
        System.out.println(o);
    }

}





package src;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: swrite
 * Date: 13-4-4
 * Time: 下午5:41
 * To change this template use File | Settings | File Templates.
 */
public class testaction {
    private static void  p(Object obj){
             System.out.println(obj);
    }

         public static void main(String args[]){


             /**
              * 认识\s \w \d \

             p(" \n\r\t".matches("\\s{4}"));
             p(" ".matches("\\S"));
             p("a_8".matches("\\w{3}"));
             p("abc888&^%".matches("[a-z]{1,3}\\d+[&^#%]+"));
             p("\\".matches("\\\\"));


             //边界处理
             p("hello sir".matches("^h.*"));
             p("hello sir".matches(".*ir$"));
             p("hello sir".matches("^h[a-z]{1,3}o\\b.*"));
             p("hellosir".matches("^h[a-z]{1,3}o\\b.*"));
             //匹配空白行
             p(" \n".matches("[\\s&&[^\\n]]*\\n$"));    */
//           p("asdfasdfsafsf@dsdfsdf.com".matches("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+"));
//           p("swrite@qq.com".matches("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+"));
//             Pattern p = Pattern.compile("\\d{3,5}");
//             String s = "123-34345-234-00";
//             Matcher m = p.matcher(s);
//             p(m.matches());
//             p(m.reset());
//             p(m.find());
//             p(m.start()+"-"+m.end());
//             p(m.find());
//             p(m.start()+"-"+m.end());
//             p(m.find());
//             p(m.start()+"-"+m.end());
//             p(m.find());
//             p(m.lookingAt());
//             p(m.lookingAt());
//             p(m.lookingAt());
//             p(m.lookingAt());

/*                Pattern p=Pattern.compile("java",Pattern.CASE_INSENSITIVE);
                Matcher m=p.matcher("java Java JAVa JaVa IloveJAVA you hateJava afasdfasdf");
                StringBuffer buf=new StringBuffer();
                int i=0;
                while (m.find()){
                    i+=1;
                    if(i%2==0){
                        m.appendReplacement(buf,"java");
                    } else{
                        m.appendReplacement(buf,"JAVA");
                    }
                }
                m.appendTail(buf);
                p(buf);*/

//             Pattern p=Pattern.compile("(\\d{3,5})([a-z]{2})");
////             Pattern p=Pattern.compile("(\\d{3,5})([a-z]{2})");
//             String s = "123aa-34345bb-234cc-00";
//             Matcher m=p.matcher(s);
//            while(m.find()){
//                p(m.group(1));
//            }

           /*  Pattern p=Pattern.compile(".{3,10}[0-9]");     //一口全部吞进去,然后不合适在一个一个吐,例如:一下子把aaaa1bbbb2 吞进去
             Pattern p2=Pattern.compile(".{3,10}?[0-9]");   //一小口一小口吞,首先吞3个字母,然后后面的不匹配接着找数字
             Pattern p3=Pattern.compile(".{3,10}+[0-9]");   //一口吞进去了就不吐了

             String test="aaaa1bbbb2";
             String test_3="aaaa1bbbb21";
             Matcher m=p.matcher(test);
             Matcher m2=p2.matcher(test);
             Matcher m3=p3.matcher(test);
             Matcher m3_1=p3.matcher(test_3);
             if(m.find()){
                 System.out.println(m.start()+"-"+m.end());
             }
             if(m2.find()){
                 System.out.println(m2.start()+"-"+m2.end());
             }
             if(m3.find()){
                 System.out.println(m3.start()+"-"+m3.end());
             }else{
                 p("not found");
             }if(m3_1.find()){
                 System.out.println(m3_1.start()+"-"+m3_1.end());
             }else{
                 p("not found");
             }
*/
            /* Pattern p=Pattern.compile(".{3}a$");
             String a="abca";
             Matcher m=p.matcher(a);
             while(m.find()){
                 p(m.group());
             }*/

            Pattern p=Pattern.compile(".{3}(?=a)");//前面跟着3个字母,且是以a 结束的,但是不包括a
             String a="44a66b";
             Matcher m=p.matcher(a);
             while(m.find()){
                 p(m.group());
             }
             Pattern p2=Pattern.compile("(?=a).{3}");//以a 为头,输入三个字母,若符合条件就捕获abc
             String a2="44a66b";
             Matcher m2=p2.matcher(a2);
             while(m2.find()){
                 p(m2.group());
             }
             Pattern p3=Pattern.compile("(?!a).{3}");//前面不能是a
             String a3="44a66b";
             Matcher m3=p2.matcher(a3);
             while(m3.find()){
                 p(m3.group());
             }
             Pattern p4=Pattern.compile(".{3}(?!a)");//后面跟着的不能是a
             String a4="44a66b";
             Matcher m4=p2.matcher(a4);
             while(m4.find()){
                 p(m4.group());
             }

            Pattern p5=Pattern.compile(".{3}(?<!a)");//从后面往前读, 不能是a 的    ||.{3}(?<=a)  是a d的,则输出44a,a66 . 若写在前面则不算进去a,写在后则算进去a
             String a5="44a66b";
             Matcher m5=p2.matcher(a5);
             while(m5.find()){
                 p(m5.group());
             }

             Pattern p6=Pattern.compile(".{3}(?<=a)");//后面跟着的不能是a
             String a6="44a66b";
             Matcher m6=p2.matcher(a6);
             while(m6.find()){
                 p(m6.group());
             }

             Pattern px = Pattern.compile("(\\d(\\d))\\2");       //指 找到第一组12 两个数字之后,后面的再一组还必须和第一组一样
             String s = "122";
             Matcher mx = p.matcher(s);
             p(mx.matches());

             Pattern pa = Pattern.compile("java",Pattern.CASE_INSENSITIVE);
             p("Java".matches("(?i)(java)"));// 上面方法的简写


         }


}
原文链接:https://www.f2er.com/regex/362932.html

猜你在找的正则表达式相关文章