首页 > 代码库 > 正则表达式
正则表达式
申明:正则表达式可以大大的简化代码,不过对于看不懂的人来说,那也只能骂娘。切身体会,所以感觉有必要撸下来正则表达式!(后期会不断添加各种正则判断)
基本概念不多说我,直接上例子,通过例子说明吧。
一、正则基础
Demo1:
在java中对反斜线\的处理与其它语言不同,在其它语言中,\\表示“我想要在正则表达式中插入一个普通的(字面上的)反斜线”,请不要给它任何特殊的意义。而在java中,\\的意思是“我要插入一个正则表达式的反斜线,所以其后的写符具有特殊的意义。"例如,如果我想要表示一位数字,那么正则表达式应该是\\d。如果你想插入一个普通的反斜线,则应该这样\\\\,不过换行和制表之类的东西只需要使用单反斜线:\n\t
package com.rah;/*** * * @author team * */public class Demo1 { public static void main(String[] args) { /*** * 反斜线\在程序中必须以\\表示如下: */ System.out.println("\\".matches("\\\\")); /*** * ("-?\\d+")匹配:可能有一个负号,或者后面跟着一位或多位数字 */ System.out.println("-1234".matches("-?\\d+")); System.out.println("5678".matches("-?\\d+")); System.out.println("+911".matches("-?\\d+")); /*** * ("(-|\\+)?\\d+")匹配:表示字符串的起始字符可能是一个-或者+(+有特殊意义,需要用\\转义),后面跟着一位或多位数字 */ System.out.println("+911".matches("(-|\\+)?\\d+")); }}
运行结果:
truetruetruefalsetrue
Demo2
String.spilt是一个非常有用的正则表达式工具,其功能是”将字符串从正则表达式匹配的地方切开“。
package com.rah;import java.util.Arrays;/*** * * @author team * */public class Demo2 { public static String knights = "Then, when you have found the shrubbery, you must " + "cut down the mightiest tree in the forest..." + "with... a herring!"; public static void split(String regex) { System.out.println(Arrays.toString(knights.split(regex))); } public static void main(String[] args) { /*** * 按空格划分字符串 */ split(" "); /*** * \W(\\W转义)意思是非单词字符如果是小写W,\w则表示一个单词字符 * 该正则可以标点字符给删了 */ split("\\W+"); /*** * 字母n后面跟着一个单词字符 */ split("n\\W+"); }}
运行结果:
[Then,, when, you, have, found, the, shrubbery,, you, must, cut, down, the, mightiest, tree, in, the, forest...with..., a, herring!][Then, when, you, have, found, the, shrubbery, you, must, cut, down, the, mightiest, tree, in, the, forest, with, a, herring][The, whe, you have found the shrubbery, you must cut dow, the mightiest tree i, the forest...with... a herring!]
Demo3
String.replaceFirst()/replaceAll(),也是可以匹配正则的
package com.rah;/*** * * @author team * */public class Demo3 { public static String sqlOne = "select * from students"; public static String sqlTwo = "seelct count(*) from students"; public static void main(String[] args) { /*** * 从前四个输出可以看出[]里面是只匹配他就会在第一时间匹配到就不会往下找,就是因为这个小知识点,在开发中浪费了我好多时间Q_Q */ System.out.println(sqlOne.replaceFirst("s", "count(*)")); System.out.println(sqlOne.replaceFirst("[s]", "count(*)")); /*** * 找到se匹配 */ System.out.println(sqlOne.replaceFirst("se", "count(*)")); /*** * 找到s匹配,就不会往下找 */ System.out.println(sqlOne.replaceFirst("[se]", "count(*)")); System.out.println(sqlOne.replaceFirst("[*]", "count(*)")); System.out.println(sqlTwo.replaceFirst("count\\(\\*\\)", "*")); }}
运行结果:
count(*)elect * from studentscount(*)elect * from studentscount(*)lect * from studentscount(*)elect * from studentsselect count(*) from studentsseelct * from students
Demo4:
检查句子以大写字母开头、以句号结尾
package com.rah;/*** * * @author team * */public class Demo4 { public static boolean matches(String text) { /*** * \\p{javaUpperCase} 大写字母,不明白的可以看jdk文档 */ return text.matches("\\p{javaUpperCase}.*\\."); } public static void main(String[] args) { System.out.println(matches("This is correct.")); System.out.println(matches("bad sentence 1.")); System.out.println(matches("Bad sentence 2")); System.out.println(matches("This is also correct...")); }}
运行结果:
truefalsefalsetrue
Demo5:
package com.rah;import java.util.Arrays;/*** * * @author team * */public class Demo5 { public static String knights = "Then, when you have found the shrubbery, you must " + "cut down the mightiest tree in the forest..." + "with... a herring!"; public static void split(String regex) { System.out.println(Arrays.toString(knights.split(regex))); } public static void main(String[] args) { /*** * 在the和you处分割 */ split("the|you"); }}
运行结果:
[Then, when , have found , shrubbery, , must cut down , mightiest tree in , forest...with... a herring!]
Demo6
package com.rah;/*** * * @author team * */public class Demo5 { public static String knights = "Then, when you have found the shrubbery, you must " + "cut down the mightiest tree in the forest..." + "with... a herring!"; /* * 对应的内嵌标志表达式是 (?i),它有四种形式: * 1,(?i) * 2,(?-i) * 3,(?i:X) * 4,(?-i:X) * 不带有 - 的是开标志,带有 - 的是关标志。 */ public static void main(String[] args) { /*** * 对book都忽略大写 */ System.out.println("Book".matches("(?i)Book")); /*** * 对b都忽略大写,ook还是得比较大小写,下面的方法作用一样,写的更简洁 */ System.out.println("Book".matches("(?i)b(?-i)ook")); /*** * 对b都忽略大写,ook还是得比较大小写 */ System.out.println("Book".matches("(?i:b)ook")); /*** * (?-i) 的作用域是前面,如a(?-i) (?-i)的作用域是后面,如(?i)B */ System.out.println("bOOk".matches("b(?-i)(?i)ook")); System.out.println("aBook".matches("a(?-i:B)ook")); /*** * [] 只要匹配到一个再往下匹配,匹配不到了就把当前的替换 * 没有[] 他要满足字符串到匹配到才换 */ System.out.println("ouahoahuah".replaceAll("[ou]", "")); System.out.println("ouahoahuah".replaceAll("ou", "")); /*** * 忽略大小写匹配aeiou */ System.out.println(knights.replaceAll("(?i)[aeiou]", "")); }}
运行结果:
truetruetruetruetrueahahahahoahuahThn, whn y hv fnd th shrbbry, y mst ct dwn th mghtst tr n th frst...wth... hrrng!
二、创建正则表达式
写法参考java.util.regex包下的Pattern类
Demo7:
package com.rah;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Demo7 { public static void main(String[] args) { if(args.length<2){ System.out.println("Usage:\njava TestRegularExpression " + "characterSequence regularExpression"); System.exit(0); } System.out.println("Input: \"" + args[0] + "\""); for(String arg : args){ System.out.println("Regulqr expression: \"" +arg +"\""); Pattern p = Pattern.compile(arg); Matcher m = p.matcher(args[0]); while(m.find()){ System.out.println("Match \"" + m.group() + "\" at position " + m.start() + "-" + (m.end()-1)); } } }}
传入的参数:
abcabcabcdefabc abc+ (abc)+ (abc){2,}
运行结果:
Input: "abcabcabcdefabc"Regulqr expression: "abcabcabcdefabc"Match "abcabcabcdefabc" at position 0-14Regulqr expression: "abc+"Match "abc" at position 0-2Match "abc" at position 3-5Match "abc" at position 6-8Match "abc" at position 12-14Regulqr expression: "(abc)+"Match "abcabcabc" at position 0-8Match "abc" at position 12-14Regulqr expression: "(abc){2,}"Match "abcabcabc" at position 0-8
未完待续。。
正则表达式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。