加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式

发布时间:2020-12-13 22:52:25 所属栏目:百科 来源:网络整理
导读:字符规则,例如在我的电脑搜寻*.pdf 各种符号需要理解 判断一段文字是否为数字 主要在regex和String这俩个包 pattern complie //编译正则表达式 Pattern p=Pattern.compile("[0-9]+"); //这个表示从1到9,+代表一个或多个元素 Pattern p = Pattern.("a*b");
字符规则,例如在我的电脑搜寻*.pdf
各种符号需要理解
判断一段文字是否为数字 主要在regex和String这俩个包
pattern complie

//编译正则表达式

Pattern p=Pattern.compile("[0-9]+"); //这个表示从1到9,+代表一个或多个元素

 Pattern p = Pattern.("a*b");
 Matcher m = p.("aaaaab");
 boolean b = m.();
compilematchermatches


模式一 验证
String str= "1994-07-27" ;
Stringpa= "d{4}-d{2}-d{2}" ; /* d代表数字,{4}代表四次*/
Pattern p=Pattern.compile(pa);
Matcher m=p.matcher(str);
System. out .println(m.matches());


模式二 替换
String str="a15df121fd511gdg151f1g51dggg11fd55";
String pa= "d+" ;
Pattern p=Pattern.compile(pa);
Matcher m=p.matcher(str);
String newString=m.replaceAll( "-" );
System. out .println(newString);


模式三 提取,分割
Stringstr="a15df121fd511gdg151f1g51dggg11fd55";
Stringpa= "d+" ;
Pattern p=Pattern.compile(pa);
Matcher m=p.matcher(str);
String[] r=p.split(str);
for (Stringr1:r){
System. out .println(r1);
}
模式四 更细节的操作

matches() 与 find() 方法 加上start() end() 方法的结合
find():尝试查找与该模式匹配的输入序列的下一个子序列。
group(intgroup)
返回在以前匹配操作期间由给定组捕获的输入子序列。
Pattern p=Pattern.compile( "d{3,5}" );
String s= "123-45678-901-00-123-456" ;
Matcher m=p.matcher(s);
if (m.matches()){
System. out .println( "当前位置:" +m.start()+ "-" +m.end());
}
m.reset(); //使用完match后,要将状态回复一下
while (m.find()){
System. out .println( "当前位置:" +m.start()+ "-" +m.end());
System. out .println(m.group()); //取结果
}


模式五 Pattern 参数 CASE_INSENSITIVE(忽略大小写),多行多列(MUTILINE)
Pattern p = Pattern.compile("china",Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher( "CHINA china This is ChIna n nia CHinA" );
System.out.println(m.replaceAll("china"));

模式六 String也来尝试正则表达式
String str1= "A1B22C333D4444E8888F" .replaceAll( "d+" , "-" );
boolean temp= "1998-07-06" .matches( "d{4}-d{2}-d{2}" );
String s[]= "A1B22C33D4444E5555F" .split( "d+" );
System. out .println( "字符串的替换" +str1);
System. out .println( "字符串的比较" +temp);
for ( int i=0;i<s. length ;i++){
System. out .print(s[i]+ "t" );
}

模式六 group 新

Pattern p=Pattern.compile("(d{3,5})([a-z]{2})");
String s= "123aa-45678aa-901aa-00-123ff-456gfd" ;
Matcher m=p.matcher(s);
if (m.matches()){
System. out .println( "当前位置:" +m.start()+ "-" +m.end());
}
m.reset(); //使用完match后,要将状态回复一下
while (m.find()){
System. out .println( "当前位置:" +m.start()+ "-" +m.end());
System. out .println(m.group()+ "t" +m.group(1)+ "t" +m.group(2)); //取结果
}

模式七 网上传输数据

String info= "1张三05-2李四16-3李达19" ;
Pattern p=Pattern.compile( "(d+)([u4e00-u9fa5]{2,5})(d{2})" );
Matcher m=p.matcher(info);
while (m.find()){
System. out .println(m.group(1)+ "t" +m.group(2)+ "t" +m.group(3));
}

模式八 小结

System.out.println("a".matches("."));
System. out .println( "aa" .matches( "aa" ));
System. out .println( "aa" .matches( "a*" ));
System. out .println( "aaaaaaaaaa" .matches( "a+" ));
System. out .println( "" .matches( "a" ));
System. out .println( "" .matches( "aa" ));
System. out .println( "" .matches( "a*" )); /*true*/
System. out .println( "" .matches( "a+" ));
System. out .println( "" .matches( "a?" )); /*ture*/
System. out .println( "a" .matches( "a?" ));
/* X? X,一次或一次也没有
X* X,零次或多次 */



正则表达式的常用点

边界匹配器
^ 行的开头
$ 行的结尾

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



字符类
[abc] abc(简单类)
[^abc] 任何字符,除了abc(否定)
[a-zA-Z] azAZ,两头的字母包括在内(范围)
[a-d[m-p]] admp[a-dm-p](并集)
[a-z&&[def]] def(交集)
[a-z&&[^bc]] az,除了bc[ad-z](减去)
[a-z&&[^m-p]] az,而非mp[a-lq-z](减去)

分行
Pattern.MULTILINE multi-line
从每一行中取单词
split() => String[]

循环这个数组,查找最大的度的单词 .length
String s= "I have a dream,A greate DreamI open my eye last night. with all my love is Kingston garden. <The young Victoria> I would tell you to go to the hell,But I think you are already there. <king of war>.This asdfdsazsads's how are you donngn" ;
Patternp= Pattern.compile( "s" ,Pattern. MULTILINE );
String[] ss=s.split( "[^a-zA-Z]" );
int max=0;
for ( int i=0;i<ss. length ;i++){
if (ss[i].length()>max){
max=i;
}
}
System.out.println("最长为:"+ss[max]);



影哥语录:这个对于以后的密码验证还有邮箱验证等等很重要,能省去很多的时间。



[正则表达式]文本框输入内容控制 整数或者小数:^[0-9]+.{0,1}[0-9]{0,2}$ 只能输入数字:"^[0-9]*$"。 只能输入n位的数字:"^d{n}$"。 只能输入至少n位的数字:"^d{n,}$"。 只能输入m~n位的数字:。"^d{m,n}$" 只能输入零和非零开头的数字:"^(0|[1-9][0-9]*)$"。 只能输入有两位小数的正实数:"^[0-9]+(.[0-9]{2})?$"。 只能输入有1~3位小数的正实数:"^[0-9]+(.[0-9]{1,3})?$"。 只能输入非零的正整数:"^+?[1-9][0-9]*$"。 只能输入非零的负整数:"^-[1-9][]0-9"*$。 只能输入长度为3的字符:"^.{3}$"。 只能输入由26个英文字母组成的字符串:"^[A-Za-z]+$"。 只能输入由26个大写英文字母组成的字符串:"^[A-Z]+$"。 只能输入由26个小写英文字母组成的字符串:"^[a-z]+$"。 只能输入由数字和26个英文字母组成的字符串:"^[A-Za-z0-9]+$"。 只能输入由数字、26个英文字母或者下划线组成的字符串:"^w+$"。 验证用户密码:"^[a-zA-Z]w{5,17}$"正确格式为:以字母开头,长度在6~18之间,只能包含字符、数字和下划线。 验证是否含有^%&',;=?$"等字符:"[^%&',;=?$x22]+"。 只能输入汉字:"^[u4e00-u9fa5]{0,}$" 验证Email地址:"^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$"。 验证InternetURL:"^http://([w-]+.)+[w-]+(/[w-./?%&=]*)?$"。 验证电话号码:"^((d{3,4}-)|d{3.4}-)?d{7,8}$"正确格式为:"XXX-XXXXXXX"、"XXXX-XXXXXXXX"、"XXX-XXXXXXX"、"XXX-XXXXXXXX"、"XXXXXXX"和"XXXXXXXX"。 验证身份证号(15位或18位数字):"^d{15}|d{18}$"。 验证一年的12个月:"^(0?[1-9]|1[0-2])$"正确格式为:"01"~"09"和"1"~"12"。 验证一个月的31天:"^((0?[1-9])|((1|2)[0-9])|30|31)$"正确格式为;"01"~"09"和"1"~"31"。 匹配中文字符的正则表达式: [u4e00-u9fa5]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读