public class RegexDemo {
public static void main(String[] args) {
checkQQ();
<span style="font-family:Microsoft YaHei;"> }</span>
public static void checkQQ()
{
String qq="32430";
String Regex = "[1-9][0-9]{4,14}"; //第一位是1-9,第二位是0-9,控制在4-14个
//String Regex = "[1-9]\\d{4,14}";
boolean flag = qq.matches(Regex);
if(flag)
System.out.println(qq+"合法");
else
System.out.println(qq+"不合法");
}
}
public class RegexDemo {
public static void main(String[] args) {
spiltDemo( "xiaoming xiaoqiang xiaohong", " +");//按照多个空格来切割
<span style="font-family:Microsoft YaHei;"> }</span>
public static void spiltDemo(String str,String reg)
{
<span style="font-family:Microsoft YaHei;"></span>
String [] arr = str.split(reg);
System.out.println(arr.length);
for (String s: arr)
{
System.out.println(s);
}
}
<span style="font-family:Microsoft YaHei;">}</span>
public class RegexDemo {
public static void main(String[] args) {
String str1= "sdkaj234567dsflk5678";
String regex1 ="\\d{1,}";
String replacement1 = "#"; //将字符串中数字替换成#
replaceAllDemo(str1, regex1, replacement1);
String str2= "sdhhhjollll";
String regex2 ="(.)\\1+"; //.代表任意字符
String replacement2 = "$1"; //重复的字符替换成单个字符 hhh-->h
replaceAllDemo(str2,regex2,replacement2);
}
public static void replaceAllDemo(String str, String regex, String replacement) //替换方法
{
str = str.replaceAll(regex, replacement);
System.out.println(str);
}
<span style="font-family:Microsoft YaHei;">}</span>
程序运行结果:import java.util.regex.*;
public class RegexDemo2 {
public static void main(String[] args) {
getDemo();
}
public static void getDemo()
{
String str = "wo yao tong zhi shi jie ";
String reg = "\\b[a-z]{4}\\b"; //获取连续四个字符组成的单词,\\b表示单词边界
Pattern p = Pattern.compile(reg); //将规则封装成对象
Matcher m = p.matcher(str) ; //让正则对象和要作用的字符串相关联,获取匹配器对象
while (m.find()) //将规则作用到字符串上,并进行符合规则的子串查找
{
System.out.println(m.group()); //获取匹配后结果
System.out.println(m.start()+"......"+m.end());; //获取开始和结束的角标
}
}
}程序运行结果:public class RegexTest {
public static void main(String[] args) {
<span style="font-family:Microsoft YaHei;"> </span> ipsort();
checkMail();
}
public static void ipsort()
{
String ip = "192.168.1.34 102.67.46.1 45.78.1.0 35.156.229.56";
ip = ip.replaceAll("(\\d+)", "00$1"); //将一个或多个数字替换成00加原数字
System.out.println(ip);
ip= ip.replaceAll("0*(\\d{3})","$1"); //将00192等替换成192
System.out.println(ip);
String[] arr = ip.split(" +"); //按空格切割
TreeSet<String> ts = new TreeSet<String>(); //利用TreeSet 按自然顺序排序
for (String s : arr)
{
ts.add(s); //将arr中切割过的字符串添加到ts中
}
for(String s : ts)
{
System.out.println(s.replaceAll("0*(\\d+)","$1")); //将001等还原为1
}
}
/*
* 需求:对邮箱地址校验
* */
public static void checkMail()
{
String mail = "hpu186@163.com.cn";
String reg = "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+)+"; //精确匹配
// String reg ="\\w+@\\w+(\\.\\w+)+"; //相对粗糙的匹配
System.out.println(mail.matches(reg));
}
}运行结果:import java.io.*;
import java.net.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexText2 {
public static void main(String[] args) throws Exception
{
getMails_1();
getMails_2();
}
public static void getMails_2() throws Exception //获取指定网页中邮箱地址
{
URL url = new URL("http://zhidao.baidu.com/question/518382624466008525.html?fr=iks&word=%D3%CA%CF%E4%C3%FB&ie=gbk");
URLConnection conn = url.openConnection();
BufferedReader bufIn = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
String regex ="\\w+@\\w+(\\.\\w+)+";
Pattern p = Pattern.compile(regex);
while((line =bufIn.readLine())!=null) //每读一行就匹配一次
{
Matcher m = p.matcher(line) ;
while(m.find())
{
System.out.println(m.group());
}
}
bufIn.close();
}
public static void getMails_1() throws Exception //获取指定文件中邮箱地址
{
BufferedReader bufr = new BufferedReader(new FileReader("mail.txt"));
String line = null;
String regex ="\\w+@\\w+(\\.\\w+)+";
Pattern p = Pattern.compile(regex);
while((line =bufr.readLine())!=null) //每读一行就匹配一次
{
Matcher m = p.matcher(line) ;
while(m.find())
{
System.out.println(m.group());
}
}
bufr.close();
}
}原文地址:http://blog.csdn.net/hpu186/article/details/45566789