正则表达式

来源:互联网 发布:淘宝卖衣服赚钱吗 编辑:程序博客网 时间:2024/06/16 08:12

String里支持的正则表达式

  1. boolean matches(String regex):判断该字符串指定的正则表达式。
  2. String replaceAll(String regex,String replacement):将该字符串中所有匹配regex的子串替换成replacement。
  3. String replaceFirst(String regex,String replacement):将该字符串中第一个匹配regex的子串替换成replacement。
  4. String[] split(String regex):以regex作为分隔符,把该字符串分割成多个子串

创建正则表达式

字符

\\ 反斜杠\t 间隔 ('\u0009')\n 换行 ('\u000A')\r 回车 ('\u000D')\d 数字等价于[0-9]\D 非数字等价于[^0-9]\s 空白符号 [\t\n\x0B\f\r]\S 非空白符号 [^\t\n\x0B\f\r]\w 单独字符 [a-zA-Z_0-9]\W 非单独字符 [^a-zA-Z_0-9]\f 换页符\e Escape\b 一个单词的边界\B 一个非单词的边界\G 前一个匹配的结束\A:输入的开头\Z:输入的结尾,仅用于最后的结束符\z:输入的结尾^为限制开头,匹配一行的开头。要匹配^字符本身,请使用\^;^java 条件限制为以Java为开头字符$为限制结尾,匹配一行的结尾。要匹配$字符本身,请使用\$;():标记子表达式的开始和结束位置。要匹配这些字符,请使用和;[]:用于确定中括号表达式的开始和结束位置。要匹配这些字符,请使用java$ 条件限制为以java为结尾字符. 条件限制除\n以外任意一个单独字符java.. 条件限制为java后除换行外任意两个字符\0mnn:八进制数Omnn所表示的字符;\xhh:十六进制0xhh所表示的字符;\uhhhh:十六进制0xhhhh所表示的UNICODE字符;{}:用于标记前面子表达式的出现的频度。要匹配这些字符,请使用\{\};*:指定前面子表达式可以出现零次或多次。要匹配*字符本身,请使用\*;+:指定前面子表达式可以出现一次或多次。要匹配+字符本身,请使用\+;?:指定前面子表达式可以出现零次或一次。要匹配?字符本身,请使用\?;.:匹配除换行符\n之外的任何单位字符。要匹配,字符本身,请使用\.\:用于转义下一个字符,或指定八进制、十六进制符。要匹配\字符,请使用\\;|:指定两项之间任选一项。要匹配|字符本身,请使用\|

字符类

    [abc] a、b 或 c(简单类)     [^abc] 任何字符,除了 a、b 或 c(否定)     [a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围)     [0-9] 09的字符都包括

Greedy 数量词

    X? X,一次或一次也没有    X* X,零次或多次    X+ X,一次或多次    X{n} X,恰好 n 次     X{n,} X,至少 n 次     X{n,m} X,至少 n 次,但是不超过 m 次 

String类中的三个基本操作使用正则:

  匹配:matches()  切割: split()  替换: replaceAll()

java中正则匹配的对象:

pattern:    Pattern   Pattern.complie(regexString)    Macther   Pattern.matches(regexString)Matcher:    boolean    matcher.find() //查找下一个匹配对象    String    matcher.group() //返回整个匹配模式匹配到的结果       boolean    matcher.matches() //尝试将整个区域与模式匹配    int     matcher.groupCount() //返回匹配规则的分组,如:(aa)(bb):这表示两组    String        matcher.group(int group)  //返回匹配对象对应分组的匹配结果    MatcheResult  matcher.toMatchResult()   //将匹配结果一MatchResult的形式返回    int matcher.Start():返回上一次和Pattern匹配的子串在目标字符串的开始的位置。     int matcher.End():和上面的相反返回最后的     matcher matcher.Reset():将现有的Matcher对像用于一个新的字符串序列
  1. 贪婪模式(Greedy):数量表示符默认采用贪婪模式,除非另有表示。贪婪模式的表达式会一直匹配下去,直到无法匹配为止。如果你发现表达式匹配的结果与预期的不符,很有可能是因为——你以为表达式只会匹配前面几个字符,而实际上它是贪婪模式,所以会一直匹配下去。
  2. 勉强模式(Reluctant):用问号后缀(?)表示,它只会匹配最少的字符。也称为最小匹配模式。
  3. 占有模式(Possessive):用加号后缀(+)表示,目前只有Java支持占有模式。
import java.util.regex.Matcher;import java.util.regex.Pattern;public class RegularTest {    public static void main(String[] args) {        String string = "Hello , World!";        System.out.println(string.replaceFirst("\\w*", "~"));// string.replaceFirst(regex,                                                                // replacement);        System.out.println(string.replaceFirst("\\w*?", "~"));        /*以下三行为固定模式*/        Pattern pattern = Pattern.compile("[ab]*b*[c]*");//①[]表示或,直接在后面是连接,*0个以上,+一个以上        Matcher matcher = pattern.matcher("aaaaaabbbbccccccc");//②        boolean matches = matcher.matches();//③        System.out.println(matches);        String str = "My PhoneNumber is 13500988890" + "Make Friends!contact me with 15367899876"                + "On sale!Call me 13554322345";        Pattern pat = Pattern.compile("[(13)(15)]\\d{9}");        Matcher mat = pat.matcher(str);        while (mat.find()) {            System.out.println(mat.group() + " start " + mat.start() + " end " + mat.end());        }        String[] mails = {                "asd3ff@sdf.com",                "sdfg@njmi.com",                "unii@kdmc.com"        };        /*循环体的模式*/        Pattern p = Pattern.compile("\\w^\\d+@\\w+.com");        Matcher m = null;        for (String s : mails) {            if (m == null) {                m = p.matcher(s);            } else {                m.reset(s);//将现有的Matcher对像用于一个新的字符串序列            }            System.out.println(s + (m.matches()?" is":"isn't ")+" a email address");        }    }}
package ObjectOrient;import java.util.regex.Matcher;import java.util.regex.Pattern;public class ReplaceTest {    public static void main(String[] args) {        String[] strings = {                "Compute theory is based on regular language",                "let's review what we have learned in Java",                "Learning is rejoyful"        };        Pattern pattern = Pattern.compile("re\\w*");        Matcher matcher = null;        for (String string : strings) {            if (matcher == null) {                matcher = pattern.matcher(string);            } else {                matcher.reset(string);            }            System.out.println(matcher.replaceAll("*********"));        }    }}
0 0
原创粉丝点击