正则表达式
来源:互联网 发布:手机撒谎软件下载 编辑:程序博客网 时间:2024/06/05 10:27
什么是正则表达式?
正则表达式,又称规则表达式。(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表通常被用来检索、替换那些符合某个模式(规则)的文本。
元字符
更详细的正则教程
Java 中的正则表达式
java.util.regex 包主要包括以下几个类:
- Pattern 类:Pattern 对象是一个正则表达式的编译表示。Pattern 类没有公共的构造方法,要创建一个 Pattern 对象,你必须首先调用其公共静态编译方法,它返回一个 Pattern 对象。该方法接受一下正则表达式作为它的第一个参数。
- Matcher 类:Matcher 对象是对输入字符串进行解释和匹配操作的引擎。与 Pattern 类一样,Matcher 也没有公共的构造方法。你需要调用 Pattern 对象的 matcher 方法来获得一个 Matcher 对象。
- PatternSyntaxException 异常:PatternSyntaxException 是一个非强制异常类,它表示一个正则表达式模式中的语法错误。
Pattern
Pattern.matches()
检查正则表达式模式是否匹配文本的最简单方法是使用 Pattern.matches() 静态方法:
public static void m1() { String text = "这是一个文本字符串"; String pattern = ".*文本.*"; boolean matches = Pattern.matches(pattern, text); System.out.println("matches = " + matches); // 输出 matches = true}
如果你只需要对一个文本进行一次检查时,并且 Pattern 的默认设置又符合你的要求,该方法将是你的最佳选择。
Pattern.compile()
如果需要多次匹配文本与正则表达式模式时,则需要使用 Pattern.compile() 方法创建一个实例:
public static void m2() { Pattern p = Pattern.compile("a*b");}
你也可以使用 Pattern.compile() 方法的第二个参数编译有特殊标志的 Pattern。
Pattern p = Pattern.compile("a*b", Pattern.CASE_INSENSITIVE);
Pattern 类包含一些可用于使 Pattern 匹配行为以某些方式使用的标志(int 常量)列表。上面使用的标志使得模式匹配时忽略文本大小写。
Pattern.matcher()
获取 Pattern 实例后,可以使用 matcher() 实例方法获取 Matcher 实例:
Pattern p = Pattern.compile("a*b");Matcher m = p.matcher("aaaaab");
我们可以通过 Matcher 的实例以各种方式访问文本的匹配部分。
Pattern.split()
split 可以使用正则表达式将文本分割为 string数组:
public static void m3() { String text = "A sep Text sep with sep many sep Separators"; String patternString = "sep"; Pattern pattern = Pattern.compile(patternString); String[] split = pattern.split(text); System.out.println("split.length =" + split.length); for (String element : split) { System.out.println("element =" + element); } // 输出: // split.length =5 // element =A // element = Text // element = with // element = many // element = Separators}
与分隔符匹配的文本部分不包括在返回的String数组中。
Pattern.pattern()
Pattern.pattern()方法返回 Pattern 实例被编译的模式字符串(正则表达式):
public static void m4() { Pattern pattern = Pattern.compile("a*b"); String patternStr = pattern.pattern(); System.out.println(pattern); System.out.println(patternStr); // 输出: // a*b // a*b}
其实Pattern 实例的toString()方法和pattern()方法都返回的是正则表达式。
Matcher
Matcher没有公共的构造方法,我们需要调用 Pattern 对象的 public Matcher matcher(CharSequence input) 方法来获得一个 Matcher 实例。
matches()
我们可以调用 Matcher 的实例方法 matches() 来判断文本是否与正则表达式匹配:
public static void m5() { Pattern p = Pattern.compile("a*b"); Matcher m = p.matcher("aaaaab"); boolean b = m.matches(); System.out.println(b); // 输出 true}
如果正则表达式与整个文本匹配则返回true,反之 false。
lookingAt()
matches 和 lookingAt 方法都用来尝试匹配一个输入序列模式。它们的不同是 matcher 要求整个序列都匹配,而lookingAt 不要求。lookingAt 方法虽然不需要整句都匹配,但是需要从第一个字符开始匹配。换句话说就是,如果正则表达式与文本的开头匹配,而不是整个文本,lookingAt() 则返回true,而 matches() 返回 false。
public static void m6() { String str1 = "abbbbbbbbbb"; String str2 = "cabbbbbb"; Pattern p = Pattern.compile("abbb"); Matcher m1 = p.matcher(str1); Matcher m2 = p.matcher(str2); System.out.println(m1.matches()); System.out.println(m1.lookingAt()); System.out.println(m2.lookingAt() ); // 输出 // false // true // false}
find() + start() + end()
find() 方法从匹配器区域的开头开始,如果该方法的前一次调用成功了并且从那时开始匹配器没有被重置,则从以前匹配操作没有匹配的第一个字符开始。如果匹配成功,则可以通过 start、end 和 group 方法获取更多信息。
start() 返回以前匹配的初始索引, end() 返回最后匹配字符之后的偏移量。
public static void m7() { String str = "test test testa ttt"; Pattern p = Pattern.compile("test"); Matcher m = p.matcher(str); int count = 0; while (m.find()) { System.out.println(String.format("found: %s - %s - %s", ++count, m.start(), m.end())); } // 输出 // found: 1 - 0 - 4 // found: 2 - 6 - 10 // found: 3 - 11 - 15}
reset()
该Matcher reset()方法在内部重置匹配状态Matcher。如果您通过该find()方法已经开始匹配字符串中的匹配事件,Matcher则内部将会保留关于通过输入文本搜索的距离的状态。通过调用reset()匹配将从文本的开头再次开始。
还有一种reset(CharSequence)方法。此方法将重置Matcher,并使得Matcher通过字符序列参数进行搜索,而不是Matcher创建时的字符序列了。
group()
public static void m9() { String str = "test test testa ttt"; Pattern p = Pattern.compile("(test)"); Matcher m = p.matcher(str); while (m.find()) { System.out.println(String.format("found: %s", m.group(1))); } // 输出 // found: test // found: test // found: test}
Multiple Groups
public static void m10() { String str = "test ab test cd testa ef ttt"; Pattern p = Pattern.compile("(test) (.+?) "); Matcher m = p.matcher(str); while (m.find()) { System.out.println(String.format("found: %s - %s", m.group(1), m.group(2))); } // 输出 // found: test - ab // found: test - cd}
Groups Inside Groups
public static void m11() { String str = "test ab test cd testa ef ttt"; Pattern p = Pattern.compile("((test) (.+?)) "); Matcher m = p.matcher(str); while (m.find()) { System.out.println(String.format("found: %s - %s - %s", m.group(1), m.group(2), m.group(3))); } // 输出 // found: test ab - test - ab // found: test cd - test - cd}
replaceAll() + replaceFirst()
public static void m12() { String str = "test ab test cd testa ef ttt"; Pattern p = Pattern.compile("((test) (.+?)) "); Matcher m = p.matcher(str); String replaceAll = m.replaceAll("ha "); System.out.println(replaceAll); String replaceFirst = m.replaceFirst("xi "); System.out.println(replaceFirst); // 输出 // ha ha testa ef ttt // xi test cd testa ef ttt}
appendReplacement() + appendTail()
public static void m13() { String text = "test aaa test bbb test ccc dddd"; Pattern p = Pattern.compile("((test) (.+?)) "); Matcher m = p.matcher(text); StringBuffer stringBuffer = new StringBuffer(); while (m.find()) { m.appendReplacement(stringBuffer, "ha "); System.out.println(stringBuffer.toString()); } m.appendTail(stringBuffer); System.out.println(stringBuffer.toString()); // 输出 // ha // ha ha // ha ha ha // ha ha ha dddd}
- 【正则表达式】正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- shell字符串截取
- LeetCode (Binary Tree Postorder Traversal)
- 利用动态规划求解01背包问题
- MONGODB introduction
- 华为——字串的连接最长路径查找
- 正则表达式
- oracle 存储过程心得2
- Spark查找某个IP的归属地,二分算法,try{}catch{}的使用,将结果存MySQL数据库
- 源码角度分析native层消息机制与java层消息机制的关联
- window 开发工具下载
- 九度OJ题目1206:字符串连接
- VGA
- 抽象工厂模式
- wrapping a C library with cython