标签:
String num = "13988887777\nhaha";String numRegex = "[1][3578]\\d{9}"; // 匹配手机号码boolean b = num .matches(numRegex );
public static void split() {String str1 = "itheima.java.jacob.meteor";// 按“.”分割字符串String regex1 = "\\.";String str2 = "itheima java jacob \tmeteor";// 按空白字符分割字符串String regex2 = "\\s+";String str3 = "itheimaeeejava$$jacoboooometeor";// 按重复字符分割字符串String regex3 = "(.)\\1+";split (str1 , regex1 );split (str2 , regex2 );split (str3 , regex3 );}public static void split(String str, String regex) {String[] arr = str.split(regex);for (String string : arr ) {System.out.print( string + " | ");}System.out.println();}
String str1 = "itheima######java&&&&&&&meteor";// 将将叠词替换成"-"str1 = str1 .replaceAll("(.)\\1+" , "-" );String str2 = "itheima######java&&&&&&&meteor";// 将将叠词替换成其中的一个,多个######用#替换。// 当在第二个参数中使用第一个正则参数中的组时,可以使用$编号来完成组的调用。\\1只能使用在正则表达式中。str2 = str2 .replaceAll("(.)\\1+" , "$1" );String str3 = "13988887777";// 将电话中的中间四位替换成****;str3 = str3 .replaceAll("(\\d{3})(\\d{4})(\\d{4})" , "$1****$3");
package regex;import java.util.regex.Matcher;import java.util.regex.Pattern;public class PatternDemo {public static void main(String[] args) {// 需求:获取字符串中由10个字母及以上组成的单词。String str ="Backslashes within string literals in Java source code are interpreted as required "+ "by The Java Language Specification as either Unicode escapes (section 3.3) or other "+ "character escapes (section 3.10.6) It is therefore necessary to double backslashes "+ "in string literals that represent regular expressions to protect them from "+ "interpretation by the Java bytecode compiler. The string literal b, "+ "for example, matches a single backspace character when interpreted as a regular "+ "expression, while b matches a word boundary. The string literal (hello) is illegal "+ "and leads to a compile-time error; in order to match the string (hello) the string "+ "literal (hello) must be used.";// 定义规则。String regex = "\\b[a-zA-Z]{10,}\\b";// 1. 将正则字符串编译成正则对象。Pattern p = Pattern.compile(regex );// 2. 将正则对象和字符串相关联,并获取匹配器。Matcher m = p.matcher(str);// 3. 使用find的方法/** m.lookingAt()方法从头匹配正则表达式一次。* m.start()和m.end()分别能够获取匹配字符串的头尾索引值。*/while ( m.find()) {System.out.println( m.group());}}}
package regex;import java.util.Arrays;public class Text_Regex {public static void main(String[] args) {// 需求1:匹配电子邮箱matchEmail();/** 需求2:* 把“我我.....我我.我....我.要.要要.....要.要学....学...学学....学.学编..编....编..编.编.程程.程...* ..程.程程”* 还原成:我要学编程。*/replace();/** 需求3:* 127.0.0.1 3.3.3.3 192.168.104.23 10.10.10.10* 要求按照 ip地址的分类进行从小到大的排序。*/sortIP();}public static void sortIP() {/** 思路:* 由于需要按照数字排序,而字符串排序方式为字典顺序排序,所以先补零以获得同等长度IP地址字符串,再排序,最后还原。*/String ip = "127.0.0.1 3.3.3.3 192.168.104.23 10.10.10.10";ip = ip.replaceAll( "(\\d+)", "00$1" );ip = ip.replaceAll( "0*(\\d{3})", "$1" );String[] ips = ip.split("\\s+");Arrays.sort(ips );for (String string : ips) {string = string.replaceAll( "0*(\\d+)", "$1" );System.out.println( string);}}public static void replace() {String str = "我我.....我我.我....我.要.要要.....要.要学....学...学学....学.学编..编....编..编.编.程程.程.....程.程程" ;str = str.replaceAll( "\\.+", "" );str = str.replaceAll( "(.)\\1+", "$1" );System.out.println( str);}public static void matchEmail() {String email = "jacobvv@163.com";String regex = "\\w+@[a-zA-Z0-9]+(\\.[a-zA-Z]+){1,3}" ;boolean b = email.matches( regex);System.out.println( b);}}
package regex;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.URL;import java.net.URLConnection;import java.util.HashSet;import java.util.Set;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Text_NetSpider {public static void main(String[] args) {/** 需求:网络爬虫* 获取某网页上的所有邮箱地址*/String url = "http://tieba.baidu.com/p/2720132008";Set<String> email = null;try {email = getEmail(url );} catch (IOException e) {e.printStackTrace();}for (String string : email) {System.out.println( string);}}public static Set<String> getEmail(String urlStr ) throws IOException {Set<String> email = new HashSet<String>();String regex = "\\w+@[a-zA-Z0-9]+(\\.[a-zA-Z]+){1,3}" ;Pattern p = Pattern.compile(regex );URL url = new URL( urlStr);URLConnection conn = url.openConnection();BufferedReader br = new BufferedReader( new InputStreamReader(conn.getInputStream()));String content = null;while (( content = br.readLine()) != null) {Matcher m = p.matcher(content);while ( m.find()) {email.add( m.group());}}br.close();return email;}}
String num = "13988887777\nhaha";String numRegex = "[1][3578]\\d{9}"; // 匹配手机号码boolean b = num .matches(numRegex );
public static void split() {String str1 = "itheima.java.jacob.meteor";// 按“.”分割字符串String regex1 = "\\.";String str2 = "itheima java jacob \tmeteor";// 按空白字符分割字符串String regex2 = "\\s+";String str3 = "itheimaeeejava$$jacoboooometeor";// 按重复字符分割字符串String regex3 = "(.)\\1+";split (str1 , regex1 );split (str2 , regex2 );split (str3 , regex3 );}public static void split(String str, String regex) {String[] arr = str.split(regex);for (String string : arr ) {System.out.print( string + " | ");}System.out.println();}
String str1 = "itheima######java&&&&&&&meteor";// 将将叠词替换成"-"str1 = str1 .replaceAll("(.)\\1+" , "-" );String str2 = "itheima######java&&&&&&&meteor";// 将将叠词替换成其中的一个,多个######用#替换。// 当在第二个参数中使用第一个正则参数中的组时,可以使用$编号来完成组的调用。\\1只能使用在正则表达式中。str2 = str2 .replaceAll("(.)\\1+" , "$1" );String str3 = "13988887777";// 将电话中的中间四位替换成****;str3 = str3 .replaceAll("(\\d{3})(\\d{4})(\\d{4})" , "$1****$3");
package regex;import java.util.regex.Matcher;import java.util.regex.Pattern;public class PatternDemo {public static void main(String[] args) {// 需求:获取字符串中由10个字母及以上组成的单词。String str ="Backslashes within string literals in Java source code are interpreted as required "+ "by The Java Language Specification as either Unicode escapes (section 3.3) or other "+ "character escapes (section 3.10.6) It is therefore necessary to double backslashes "+ "in string literals that represent regular expressions to protect them from "+ "interpretation by the Java bytecode compiler. The string literal b, "+ "for example, matches a single backspace character when interpreted as a regular "+ "expression, while b matches a word boundary. The string literal (hello) is illegal "+ "and leads to a compile-time error; in order to match the string (hello) the string "+ "literal (hello) must be used.";// 定义规则。String regex = "\\b[a-zA-Z]{10,}\\b";// 1. 将正则字符串编译成正则对象。Pattern p = Pattern.compile(regex );// 2. 将正则对象和字符串相关联,并获取匹配器。Matcher m = p.matcher(str);// 3. 使用find的方法/** m.lookingAt()方法从头匹配正则表达式一次。* m.start()和m.end()分别能够获取匹配字符串的头尾索引值。*/while ( m.find()) {System.out.println( m.group());}}}
package regex;import java.util.Arrays;public class Text_Regex {public static void main(String[] args) {// 需求1:匹配电子邮箱matchEmail();/** 需求2:* 把“我我.....我我.我....我.要.要要.....要.要学....学...学学....学.学编..编....编..编.编.程程.程...* ..程.程程”* 还原成:我要学编程。*/replace();/** 需求3:* 127.0.0.1 3.3.3.3 192.168.104.23 10.10.10.10* 要求按照 ip地址的分类进行从小到大的排序。*/sortIP();}public static void sortIP() {/** 思路:* 由于需要按照数字排序,而字符串排序方式为字典顺序排序,所以先补零以获得同等长度IP地址字符串,再排序,最后还原。*/String ip = "127.0.0.1 3.3.3.3 192.168.104.23 10.10.10.10";ip = ip.replaceAll( "(\\d+)", "00$1" );ip = ip.replaceAll( "0*(\\d{3})", "$1" );String[] ips = ip.split("\\s+");Arrays.sort(ips );for (String string : ips) {string = string.replaceAll( "0*(\\d+)", "$1" );System.out.println( string);}}public static void replace() {String str = "我我.....我我.我....我.要.要要.....要.要学....学...学学....学.学编..编....编..编.编.程程.程.....程.程程" ;str = str.replaceAll( "\\.+", "" );str = str.replaceAll( "(.)\\1+", "$1" );System.out.println( str);}public static void matchEmail() {String email = "jacobvv@163.com";String regex = "\\w+@[a-zA-Z0-9]+(\\.[a-zA-Z]+){1,3}" ;boolean b = email.matches( regex);System.out.println( b);}}
package regex;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.URL;import java.net.URLConnection;import java.util.HashSet;import java.util.Set;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Text_NetSpider {public static void main(String[] args) {/** 需求:网络爬虫* 获取某网页上的所有邮箱地址*/String url = "http://tieba.baidu.com/p/2720132008";Set<String> email = null;try {email = getEmail(url );} catch (IOException e) {e.printStackTrace();}for (String string : email) {System.out.println( string);}}public static Set<String> getEmail(String urlStr ) throws IOException {Set<String> email = new HashSet<String>();String regex = "\\w+@[a-zA-Z0-9]+(\\.[a-zA-Z]+){1,3}" ;Pattern p = Pattern.compile(regex );URL url = new URL( urlStr);URLConnection conn = url.openConnection();BufferedReader br = new BufferedReader( new InputStreamReader(conn.getInputStream()));String content = null;while (( content = br.readLine()) != null) {Matcher m = p.matcher(content);while ( m.find()) {email.add( m.group());}}br.close();return email;}}
标签:
原文地址:http://blog.csdn.net/u010388781/article/details/51167764