Java: Pattern and Matcher
In addition to the regular expression methods that are available in the String class (see String Regular Expressions), there are two classes that are specifically user for regular expression matching.
Common methods
The following variables represent elements of their declared type in the prototypes below.
import java.util.regex.*;
. . .
boolean b; // may be used in if statement.
int i; // index or character position.
int g; // group number
int n; // number of groups
CharSequence cs; // effectively either String or StringBuffer
String s; // Subject string.
String regex; // Regular expression string.
StringBuffer sb; // Used to build up string from repeated scans.
Pattern p = Pattern.compile(regex); // Compiles regular expression into Pattern.
Matcher m = p.matcher(s); // Creates Matcher with subject s and Pattern p.
Result
Method
Description
Creating a Pattern |
p = |
Pattern.compile(regex); |
Creates Pattern based on regex. May throw PatternSyntaxException . |
p = |
Pattern.compile(regex, f); |
As above. Flag f can be Pattern.CASE_INSENSITIVE , .... |
Finding pattern matches |
b = |
m.find(); |
True if pattern can be found in the subject string. First call starts trying at beginning of string. Subsequent calls start after last character previously matched, which makes it good for a while loop. |
b = |
m.find(i); |
True if pattern can match somewhere at or after position i. |
b = |
m.matches(); |
True if pattern matches entire subject string. |
b = |
Pattern.matches(regex, s); |
As above, but less efficient if regex used more than once. |
b = |
m.lookingAt(); |
True if pattern matches starting at first char. |
Getting results of last pattern match. Corresponds to group 0, then entire match. |
s = |
m.group(); |
String which was matched. |
i = |
m.start(); |
Index of first character of match. |
i = |
m.end(); |
Index of last character plus 1 of match. |
Getting group results of last match |
s = |
m.group(g); |
String which was matched by group g . |
i = |
m.start(g); |
Index of first character of group g. |
i = |
m.end(g); |
Index of last character plus 1 of group g. |
n = |
m.groupCount(); |
Number of groups that were matched. |
Misc |
m = |
m.reset(); |
Resets Matcher m so that next find starts at beginning. |
m = |
m.reset(cs); |
Resets Matcher m to match subject cs . |
Replacing text |
s = |
m.replaceFirst(rep); |
Returns string which is subject with first match replaced by rep . |
s = |
m.replaceAll(rep); |
Returns string with all matches replaced by rep . |
Building replacement in a StringBuffer |
m = |
m.appendReplacement(sb, s); |
When it matches, (1) everything skipped before the match is appended to the StringBuffer sb, then (2) it appends s, with "$n" replaced by group n. |
sb = |
m.appendTail(sb); |
Appends last part that m didn't match to StringBuffer. Useful after loop callingappendReplacement() to finish. |
Other Resources
分享到:
相关推荐
JAVA的正则表达式主要通过Pattern和Matcher两个核心类实现。Pattern用于编译正则表达式并存储模式,而Matcher则是执行实际匹配操作的对象。通过不同的方法,如matches、split和find,我们可以对字符串进行精确或灵活...
在Java中,正则表达式的实现主要依赖于`java.util.regex`包中的类,主要包括`Pattern`和`Matcher`两个核心类: - **Pattern**:该类用于编译正则表达式,生成Pattern对象。 - **Matcher**:该类用于对字符串执行...
你可以使用`Pattern`对象的`matcher`方法来创建一个`Matcher`对象,然后调用`Matcher`的各种方法进行匹配、查找、替换等操作。例如,找到字符串中的所有电话号码: ```java String input = "Some text with (212) ...
Matcher matcher = pattern.matcher("There are 123 apples and 456 oranges."); while (matcher.find()) { System.out.println("Found number: " + matcher.group()); } ``` 这段代码将会输出所有找到的数字: `...
在Java中,正则表达式API是通过java.util.regex包提供的,它包含了许多类和接口,如Pattern、Matcher和PatternSyntaxException等,这些使得在Java程序中使用正则表达式变得简单而高效。 ### 1. 正则表达式语法 ...
在Java中,我们可以使用`Pattern`类和`Matcher`类来处理正则表达式。`Pattern`类用于编译正则表达式,而`Matcher`类用于执行匹配操作。对于数字的匹配,我们可以使用`\d`这个正则表达式字符,它代表任何数字(等同于...
Java正则表达式是Java编程语言中的一个强大工具,它用于处理字符串,进行模式匹配和字符串查找、替换等操作。...在实际开发中,结合Java的`Pattern`和`Matcher`类,可以方便地完成字符串的匹配、替换和分割等功能。
Java的正则表达式主要由`java.util.regex`包中的三个关键类构成:`Pattern`、`Matcher`和`PatternSyntaxException`异常类。 1. **Pattern类**:该类用于编译正则表达式字符串,并创建一个Pattern对象,用于匹配文本...
描述中提到的"Tests simple Pattern compilation and Matcher methods"指出了这个项目的核心内容:测试Pattern类的编译以及Matcher类的方法。在Java中,`java.util.regex.Pattern`和`java.util.regex.Matcher`是处理...
在使用Java的`Pattern`和`Matcher`时,首先你需要创建一个`Pattern`对象,传入正则表达式字符串,然后通过`Pattern`的`matcher()`方法创建`Matcher`对象。`Matcher`提供了诸如`find()`, `matches()`, `group()`, `...
24. **Pattern and Matcher**: `java.util.regex`包中的类,用于正则表达式的匹配和操作。 25. **Queue**: 接口,表示FIFO(先进先出)的集合,提供了多种队列实现,如`LinkedList`实现的`Deque`。 26. **URI and ...
9. **StringTokenizer 和 Pattern/Matcher**: `StringTokenizer`用于分隔字符串,而`Pattern`和`Matcher`则涉及正则表达式操作,通过`compile()`编译正则表达式,`matcher()`创建匹配器,`matches()`、`find()`等...
2. **正则表达式解析**:Java的`Pattern`和`Matcher`类提供了强大的正则表达式功能,用于匹配和提取字符串中的特定模式。例如,查找所有数字: ```java String text = "There are 10 apples and 5 bananas."; ...
在Java中,`Pattern`和`Matcher`两个类提供了正则表达式的操作接口。`Pattern`类用于编译正则表达式,并创建`Matcher`对象,后者可以与目标字符串关联,执行查找、匹配、替换等操作。以下是一个简单的示例: ```...
以前写了一个java的正规表达式的java工具类,分享一下,有用到的欢迎下载使用。 如果你有常用的定义好的,且测试通过的正规表达式,欢迎跟贴,也让我享用一下 . 类中用到了 jakarta-oro-2.0.jar 包,请大家自己在 ...
`Pattern`和`Matcher`是Java中处理正则表达式的核心类。下面是一个简单示例: ```java Pattern p = Pattern.compile("java", Pattern.CASE_INSENSITIVE); // 忽略大小写 Matcher m = p.matcher(inputString); if (m...
Pattern p = Pattern.compile("java", Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(input); StringBuffer buf = new StringBuffer(); int i = 0; while (m.find()) { i++; if (i % 2 == 0) { m....
System.out.println("Found the text \"" + matcher.group() + "\" starting at index " + matcher.start() + " and ending at index " + (matcher.end() - 1) + "."); } ``` 运行结果为: - "Found the text \...
在Java中,可以使用`Pattern`和`Matcher`类来处理正则表达式。例如,创建一个`Pattern`对象,然后使用`Matcher`对象来执行匹配操作: ```java import java.util.regex.*; public class RegexExample { public ...