//split
String s = "hello world i am 23 years";
String[] ss = s.split("\\s");
//replace
s.replaceAll("\\s", "#");
s.replaceFirst("\\d", "#");
//greedy(default) & reluctant
String greedy = "\\d*";
String lazy = "\\d*?";
//Pattern & Matcher
String phone = "my phone: 13352135478. Hers is 15984563215. call us later.";
Pattern p = Pattern.compile("\\d{11}");
Matcher m = p.matcher(phone);
while(m.find()) {
System.out.printf("find: %s, start: %d, end: %d\n", m.group(), m.start(), m.end());
}
//把整个input和regexp匹配, 类似Pattern.matches(regex, input);
boolean wholeMatch = Pattern.matches("\\d{4}-\\d{7}", "0592-6103687");
//groups
String tel = "my tel is 0592-6103625. call me at 12:00";
Pattern p2 = Pattern.compile("(\\d{4})-(\\d{7})");
Matcher m2 = p2.matcher(tel);
while(m2.find()) {
int count = m2.groupCount();
for(int i=0; i<=count; i++) {
System.out.printf("group: %s. start: %d, end: %d\n", m2.group(i), m2.start(i), m2.end(i));
}
}
//静态方法
Pattern.matches("\\d{4}", "1125f"); //false
Pattern.matches("\\d{4}", "1125"); //true
//scanner
Scanner scanner = new Scanner("xushaoxun@gmail.com mail me if you have time");
System.out.println(scanner.next("(\\w+)@(\\w+)\\.(\\w{3})"));
<!----><!---->
<!---->
Regular Expression
Character Classes
Character
Classes
|
[abc]
|
a, b, or c (simple class
)
|
[^abc]
|
Any character except a, b, or c
(negation
)
|
[a-zA-Z]
|
a through z, or A through Z,
inclusive (range
)
|
[a-d[m-p]]
|
a through d, or m through p:
[a-dm-p] (union
)
|
[a-z&&[def]]
|
d, e, or f (intersection
)
|
[a-z&&[^m-p]]
|
a through z, and not m through
p: [a-lq-z] (subtraction
)
|
Predefined Character Classes
Predefined Character Classes
|
.
|
Any character (may or may not
match line terminators)
|
\d
|
A digit:
[0-9]
|
\D
|
A non-digit:
[^0-9]
|
\s
|
A whitespace character:
[ \t\n\x0B\f\r]
|
\S
|
A non-whitespace character:
[^\s]
|
\w
|
A word character:
[a-zA-Z_0-9]
|
\W
|
A non-word character:
[^\w]
|
Quantifiers
Quantifiers
|
Meaning
|
Greedy
|
Reluctant
|
Possessive
|
X?
|
X??
|
X?+
|
X
, once or not at all
|
X*
|
X*?
|
X*+
|
X
, zero or more times
|
X+
|
X+?
|
X++
|
X
, one or more times
|
X{n}
|
X{n}?
|
X{n}+
|
X
, exactly
n
times
|
X{n,}
|
X{n,}?
|
X{n,}+
|
X
, at least
n
times
|
X{n,m}
|
X{n,m}?
|
X{n,m}+
|
X
, at least
n
but not more than
m
times
|
Capturing Groups
In the expression
((A)(B(C)))
, for example, there are four such groups:
- ((A)(B(C)))
- (A)
- (B(C))
- (C)
There is also a special group,
group 0, which always represents the entire expression.
group function
public int start(int group)
public int end(int group)
public String group(int group)
Backreferences
The section of the input string
matching the capturing group(s) is saved in memory for later recall via backreference
.
A backreference is specified in the regular expression as a backslash (\
) followed by a digit indicating the
number of the group to be recalled. To match any 2 digits, followed by the
exact same two digits, you would use (\d\d)\1
as the regular expression
Boundary Matchers
Boundary Matchers
|
^
|
The beginning of a line
|
$
|
The end of a line
|
\b
|
A word boundary
|
\B
|
A non-word boundary
|
\A
|
The beginning of the
input
|
\G
|
The end of the previous
match
|
\Z
|
The end of the input but
for the final terminator, if any
|
\z
|
The end of the input
|
Pattern class
static method
Pattern.matches
(String regex, CharSequence input)
;
Pattern.compile
(String regex, int flags)
;
instance method
Matcher matcher = pattern.matcher(
CharSequence input
);
pattern.split(
CharSequence input)
;
java.lang.String equivalence
str.matches(regex);
String[] str.split(regex);
str.replace(regex, replacement);
Matcher class
Index Methods
Index methods
provide useful index values that show
precisely where the match was found in the input string:
public int start()
:
Returns the start index of the previous match.
public int start(int group)
:
Returns the start index of the subsequence captured by the given group
during the previous match operation.
public int end()
:
Returns the offset after the last character matched.
public int end(int group)
:
Returns the offset after the last character of the subsequence captured by
the given group during the previous match operation.
Study Methods
Study methods
review the input string and return a
boolean indicating whether or not the pattern is found.
public boolean lookingAt()
:
Attempts to match the input sequence, starting
at the beginning of the region, against the pattern.
public boolean find()
:
Attempts to find the next subsequence of the input sequence that matches
the pattern.
public boolean find(int start)
:
Resets this matcher and then attempts to find the next subsequence of the
input sequence that matches the pattern, starting at the specified index.
public boolean matches()
:
Attempts to match the entire region against the pattern.
Replacement Methods
Replacement methods
are useful methods for replacing
text in an input string.
public Matcher
appendReplacement(StringBuffer sb, String replacement)
:
Implements a non-terminal append-and-replace step.
public StringBuffer
appendTail(StringBuffer sb)
: Implements a
terminal append-and-replace step.
public String replaceAll(String
replacement)
: Replaces every subsequence of the
input sequence that matches the pattern with the given replacement string.
public String
replaceFirst(String replacement)
: Replaces the
first subsequence of the input sequence that matches the pattern with the
given replacement string.
public static String
quoteReplacement(String s)
: Returns a literal
replacement String
for
the specified String
.
This method produces a String
that will work as a literal replacement s
in the appendReplacement
method of the Matcher
class. The String
produced will match the sequence of characters in s
treated as a literal sequence.
Slashes ('\'
) and
dollar signs ('$'
) will
be given no special meaning.
java.lang.String
equivalence
str.replaceFirst(regex,
replacement);
str.replaceAll(regex,
replacement);
Some example:
Pattern pat = Pattern.compile
(regex
);
Matcher mat = pat.matcher(input);
while
(mat.find()) {
System.
out
.println(mat.start());
System.
out
.println(mat.end());
}
- Use matches() and lookAt()
String input =
"ofoooooooooooo"
;
String regex =
"foo"
;
Pattern pat = Pattern.compile
(regex);
Matcher mat = pat.matcher(input);
System.
out
.println
(mat.lookingAt());
//true
System.
out
.println
(mat.matches());
//false
- Use appendReplacement() and
appendTail()
Pattern p = Pattern.compile("cat");
Matcher m = p.matcher("a cat");
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb,
"dog");
}
m.appendTail(sb);
System.out.println(sb);
//just like “a cat”.replaceAll(“..”);
分享到:
相关推荐
JavaScriptRegExp对象(又称RegExp对象)是原生JavaScript中用于表示和处理正则表达式的内置对象。正则表达式是一种强大的文本...理解并熟练运用RegExp对象及其相关方法和属性,对于开发中处理文本数据是十分必要的。
JavaScript正则表达式是用于字符串检索、替换等功能的工具,它支持正则表达式的强大功能。RegExp对象是JavaScript中用于处理正则表达式的构造...通过灵活运用RegExp对象的属性和方法,可以有效提升处理字符串的能力。
总结来说,使用JavaScript中的RegExp来保留小数点后几位数的方法主要依赖于正则表达式中量词和转义字符的正确运用。通过动态构建正则表达式,我们可以实现对字符串中小数点后位数的有效控制。这种方法虽然简单,但在...
描述部分重复了三次“AS3 正则表达式”,强调本文的重点在于AS3环境下正则表达式的运用。 #### 知识点解析 以下是对给定文件中提及的各种正则表达式的详细解析: ##### 1. 匹配中文字符 ```as3 var pattern1:...
- 正则表达式可能会比简单的通配符搜索慢,因此在大数据集上使用时需谨慎。 - 可以使用索引来提高正则表达式的性能,但这通常需要创建函数索引或全文本索引。 6. **实际应用** - 数据清洗:使用正则表达式去除...
无论是简单的模式匹配还是复杂的字符串替换,这些函数都能很好地胜任。掌握这些函数不仅能提高SQL查询的能力,还能极大地提升数据处理效率。希望本文能帮助读者更好地理解和运用Oracle中的正则表达式函数。
正则表达式的灵活性和强大功能使得它在数据处理和分析中具有广泛的应用,无论是简单的文本过滤还是复杂的模式查找,都能游刃有余。 掌握正则表达式对于提升IT专业人员的数据处理能力和解决问题的效率至关重要。通过...
本文将通过具体的示例代码来详细介绍ASP中正则表达式的运用。 #### 二、ASP与正则表达式基础 在ASP中,正则表达式的使用主要依赖于`RegExp`对象。这个对象提供了多种方法来处理文本,如`Execute`用于查找匹配项,`...
7. **commons-cli**: Apache Commons CLI 提供了一个简单的方式来处理命令行选项、参数和子命令,方便创建命令行工具。 8. **backport-util-concurrent**: 这个库可能是Java并发工具的一个回移植版本,将Java 5及...
在这个“测试规范驱动指南”中,我们将深入探讨如何利用JavaScript来构建一个简单的正则表达式实现。JavaScript是Web开发中的主要脚本语言,其内置的正则表达式功能强大且灵活,对于处理字符串操作至关重要。 首先...
总之,Oracle的正则表达式是数据库管理和开发中的有力工具,通过理解和熟练运用,能大大提高数据处理的效率和精确度。无论是简单的查找、替换,还是复杂的模式匹配,都能得心应手。在学习过程中,配合实际的数据库...
4. **测试文本**:用户可以输入任何想要测试的文本,无论是一段简单的句子还是一大段文档。工具将根据提供的正则表达式进行分析,反馈匹配情况。 压缩包中的`regexp.hta`文件是该工具的主要应用程序,用户双击即可...
总结起来,Go语言的`regexp`包为开发者提供了强大而灵活的正则表达式支持,无论是简单的文本匹配还是复杂的模式处理,都能游刃有余。通过深入学习和熟练运用,你将能够更加高效地处理字符串相关的问题,提升代码的...
正则表达式是一种模式描述语言,用于定义字符串的简单或复杂模式,适用于搜索和文本操作。Oracle引入了一系列新的函数来支持正则表达式,这些函数不仅限于SQL,也适用于PL/SQL环境,并且提供了多语言支持,适用于...
C语言正则表达式库是用于在C编程环境中处理和匹配正则表达式的软件库。这个库名为PCRE(Perl Compatible Regular ...在实际开发中,结合具体的项目需求,正确理解和运用这些特性,能够极大地提高代码的效率和可维护性。
正则表达式(Regular Expression)是一种...通过理解以上知识点,你将能够运用正则表达式有效地处理HTML文本,无论是简单的查找、替换还是复杂的解析任务。但记得,针对HTML解析,使用专门的解析库通常更为稳定和高效。
LIKE操作符通常用于简单的文本匹配,它匹配整个字段中的特定模式,而正则表达式则可以在字段的任何位置进行匹配。LIKE对大小写敏感,而MySQL中的正则表达式默认是不区分大小写的,除非使用BINARY关键字。例如,`...
本文通过一个简单的实例介绍了如何使用JavaScript自定义函数来格式化时间。 首先,我们需要了解JavaScript中的`Date`对象。`Date`是JavaScript内置的日期和时间处理类,提供了获取和操作日期的方法。例如,`...
在MySQL中,正则表达式的性能可能会比简单的字符串比较慢,因此在对大量数据进行查询时,如果可能,应尽量避免使用正则表达式。然而,对于复杂的模式匹配和数据清理任务,正则表达式是不可或缺的工具。 除了`REGEXP...