/*
需求:将下列字符串改成:我要学编程
思路方式:
1,如果只想知道该字符是否对是错,使用匹配
2,想要将已有的字符串变成另外一个字符串,替换
3,想要按照自定的方式将字符串变成多个字符串,切割,获取规则以外子串
4,想要拿到符合需求的字符串子串,获取,获取符合规则的子串
*/
import java.util.*;
class RegexTest
{
public static void main(String[] args)
{
//test_1();
//ipSort();
checkMail();
}
/*
需求:对邮件地址进行校验。
*/
public static void checkMail()
{
String mail = "abc12@sina.com.cn";
mail = "1@1.1";
//出现.com.cn可以封装组重用
String reg = "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+)+";
//另一种非精确匹配 /w 单词字符:[a-zA-Z_0-9]
reg = "\\w+@\\w+(\\.\\w+)+";
//mail.indexOf("@")!=-1; 这匹配有@就行!
System.out.println(mail.matches(reg));
}
public static void test_1()
{
String str ="我我..我我..我我..要要..要要..要要..学.编程编程";
/*
将已有的字符串变成另一个字符串,使用 替换功能
1,可以先将 . 去掉。
2,再将多个重复的内容变成单个内容。
*/
str = str.replaceAll("\\.+","");
System.out.println(str);
//去叠词
str = str.replaceAll("(.)\\1+","$1");
System.out.println(str);
}
/*
192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.30
将ip地址进行地址段顺序的排序。
*/
public static void ipSort()
{
String ip = "192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.30";
ip = ip.replaceAll("(\\d+)","00$1");
System.out.println(ip);
ip = ip.replaceAll("0*(\\d{3})","$1");
System.out.println(ip);
//切割
String[] arr = ip.split(" ");
TreeSet<String> ts = new TreeSet<String>();
//遍历数组,存进集合
for(String s : arr)
{
ts.add(s);
}
// 遍历集合
for(String s : ts)
{
//替换成不带0的
System.out.println(s.replaceAll("0*(\\d+)","$1"));
}
}
}
需求:将下列字符串改成:我要学编程
思路方式:
1,如果只想知道该字符是否对是错,使用匹配
2,想要将已有的字符串变成另外一个字符串,替换
3,想要按照自定的方式将字符串变成多个字符串,切割,获取规则以外子串
4,想要拿到符合需求的字符串子串,获取,获取符合规则的子串
*/
import java.util.*;
class RegexTest
{
public static void main(String[] args)
{
//test_1();
//ipSort();
checkMail();
}
/*
需求:对邮件地址进行校验。
*/
public static void checkMail()
{
String mail = "abc12@sina.com.cn";
mail = "1@1.1";
//出现.com.cn可以封装组重用
String reg = "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+)+";
//另一种非精确匹配 /w 单词字符:[a-zA-Z_0-9]
reg = "\\w+@\\w+(\\.\\w+)+";
//mail.indexOf("@")!=-1; 这匹配有@就行!
System.out.println(mail.matches(reg));
}
public static void test_1()
{
String str ="我我..我我..我我..要要..要要..要要..学.编程编程";
/*
将已有的字符串变成另一个字符串,使用 替换功能
1,可以先将 . 去掉。
2,再将多个重复的内容变成单个内容。
*/
str = str.replaceAll("\\.+","");
System.out.println(str);
//去叠词
str = str.replaceAll("(.)\\1+","$1");
System.out.println(str);
}
/*
192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.30
将ip地址进行地址段顺序的排序。
*/
public static void ipSort()
{
String ip = "192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.30";
ip = ip.replaceAll("(\\d+)","00$1");
System.out.println(ip);
ip = ip.replaceAll("0*(\\d{3})","$1");
System.out.println(ip);
//切割
String[] arr = ip.split(" ");
TreeSet<String> ts = new TreeSet<String>();
//遍历数组,存进集合
for(String s : arr)
{
ts.add(s);
}
// 遍历集合
for(String s : ts)
{
//替换成不带0的
System.out.println(s.replaceAll("0*(\\d+)","$1"));
}
}
}