正则表达式的具体操作功能3
2015-04-17 09:42
141 查看
package regular;
import java.util.*;
public class RegexDemo3 {
/**
* @param args
* 1.如果只想知道改字符对错,用匹配。
* 2.想要将已有的字符串编程另一个字符串,替换。
* 3.想要按照自定的方式将字符串变成多个字符串,切割。(获取规则外的子串)
* 4.想要拿到符合需求的字符串子串,获取。(获取符合规则的子串)
*/
public static void main(String[] args) {
//reg_demo1();
// reg_demo2();
check_mail();
}
private static void check_mail() {
//校验邮件地址
String mail = "abc12@sina.com.cn";
String reg = "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+){1,3}";//精确匹配
String reg1 ="\\w+@\\w+(\\.\\w+){1,3}";
sop(mail.matches(reg1));
}
private static void reg_demo2() {
/*
192.68.1.254 102.49.23.103 10.10.10.10 2.2.2.2 8.109.90.30
*/
String ip = "192.68.1.254 102.49.23.103 10.10.10.10 2.2.2.2 8.109.90.30";
ip = ip.replaceAll("(\\d+)", "00$1");
sop(ip);
ip = ip.replaceAll("0*(\\d{3})", "$1");
sop(ip);
sop("------------------------------");
String[] arr = ip.split(" ");
TreeSet<String> ts = new TreeSet<String>();//排序,在这里好像没有起到作用
for(String s:arr){
ts.add(s);
}
for(String s:arr){
//s=s.replaceAll("0*(\\d+)", "$1");
sop(s);
}
}
private static void reg_demo1() {
String str = "我我...我我...我要...要要...要要...学学学...学学...编编编...编程..程.程程...程...程";
/*
想要将已有的字符串编程另一个字符串,替换。
1.先将 . 去掉
2.将多个重复内容替换为单个内容
*/
str=str.replaceAll("\\.", "");
sop(str);
str=str.replaceAll("(.)\\1+", "$1");
sop(str);
}
private static void sop(Object obj) {
System.out.println(obj);
}
}
import java.util.*;
public class RegexDemo3 {
/**
* @param args
* 1.如果只想知道改字符对错,用匹配。
* 2.想要将已有的字符串编程另一个字符串,替换。
* 3.想要按照自定的方式将字符串变成多个字符串,切割。(获取规则外的子串)
* 4.想要拿到符合需求的字符串子串,获取。(获取符合规则的子串)
*/
public static void main(String[] args) {
//reg_demo1();
// reg_demo2();
check_mail();
}
private static void check_mail() {
//校验邮件地址
String mail = "abc12@sina.com.cn";
String reg = "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+){1,3}";//精确匹配
String reg1 ="\\w+@\\w+(\\.\\w+){1,3}";
sop(mail.matches(reg1));
}
private static void reg_demo2() {
/*
192.68.1.254 102.49.23.103 10.10.10.10 2.2.2.2 8.109.90.30
*/
String ip = "192.68.1.254 102.49.23.103 10.10.10.10 2.2.2.2 8.109.90.30";
ip = ip.replaceAll("(\\d+)", "00$1");
sop(ip);
ip = ip.replaceAll("0*(\\d{3})", "$1");
sop(ip);
sop("------------------------------");
String[] arr = ip.split(" ");
TreeSet<String> ts = new TreeSet<String>();//排序,在这里好像没有起到作用
for(String s:arr){
ts.add(s);
}
for(String s:arr){
//s=s.replaceAll("0*(\\d+)", "$1");
sop(s);
}
}
private static void reg_demo1() {
String str = "我我...我我...我要...要要...要要...学学学...学学...编编编...编程..程.程程...程...程";
/*
想要将已有的字符串编程另一个字符串,替换。
1.先将 . 去掉
2.将多个重复内容替换为单个内容
*/
str=str.replaceAll("\\.", "");
sop(str);
str=str.replaceAll("(.)\\1+", "$1");
sop(str);
}
private static void sop(Object obj) {
System.out.println(obj);
}
}
相关文章推荐
- 正则表达式的具体操作功能2:
- 正则表达式的具体操作功能1:
- JAVA基础学习之IP简述使用、反射、正则表达式操作、网络爬虫、可变参数、了解和入门注解的应用、使用Eclipse的Debug功能(7)
- 通过正则表达式,为页面加上验证功能.
- mysql 正则表达式替换功能
- asp RegExp对象正则表达式功能用法
- 正则表达式中的匹配,替换,切割,获取,这四个使用时具体用哪一个,进行案例分析
- JAVA正则表达式4种常用功能
- ruby的正则表达式操作(1)
- Linux基本操作 10----- 正则表达式与文件格式化处理
- 正则表达式具体使用
- 正则表达式学习笔记1-功能介绍
- Python之re模块 - 正则表达式操作
- pycharm 替换功能 模糊匹配 使用正则表达式
- jmeter正则表达式提取器相关操作讲解
- [转]正则表达式符号--功能对照表
- 正则表达式的4种常用功能
- 常见对象_正则表达式的分割功能
- php小功能定制:对txt文本里面的字符串用正则表达式匹配
- JAVA正则表达式4种常用功能