- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Java正则表达式学习教程由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本教程旨在帮助你驾驭Java正则表达式,同时也帮助我复习正则表达式.
什么是正则表达式?
正则表达式定义了字符串的模式。正则表达式可以用来搜索、编辑或处理文本。正则表达式并不仅限于某一种语言,但是在每种语言中有细微的差别。Java正则表达式和Perl的是最为相似的.
Java正则表达式的类在 java.util.regex 包中,包括三个类:Pattern,Matcher 和 PatternSyntaxException.
Pattern对象是正则表达式的已编译版本。他没有任何公共构造器,我们通过传递一个正则表达式参数给公共静态方法 compile 来创建一个pattern对象。 Matcher是用来匹配输入字符串和创建的 pattern 对象的正则引擎对象。这个类没有任何公共构造器,我们用patten对象的matcher方法,使用输入字符串作为参数来获得一个Matcher对象。然后使用matches方法,通过返回的布尔值判断输入字符串是否与正则匹配。 如果正则表达式语法不正确将抛出PatternSyntaxException异常。 让我们在一个简单的例子里看看这些类是怎么用的吧 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
package
com.journaldev.util;
import
java.util.regex.Matcher;
import
java.util.regex.Pattern;
public
class
RegexExamples {
public
static
void
main(String[] args) {
// using pattern with flags
Pattern pattern = Pattern.compile(
"ab"
, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(
"ABcabdAb"
);
// using Matcher find(), group(), start() and end() methods
while
(matcher.find()) {
System.out.println(
"Found the text \""
+ matcher.group()
+
"\" starting at "
+ matcher.start()
+
" index and ending at index "
+ matcher.end());
}
// using Pattern split() method
pattern = Pattern.compile(
"\\W"
);
String[] words = pattern.split(
"one@two#three:four$five"
);
for
(String s : words) {
System.out.println(
"Split using Pattern.split(): "
+ s);
}
// using Matcher.replaceFirst() and replaceAll() methods
pattern = Pattern.compile(
"1*2"
);
matcher = pattern.matcher(
"11234512678"
);
System.out.println(
"Using replaceAll: "
+ matcher.replaceAll(
"_"
));
System.out.println(
"Using replaceFirst: "
+ matcher.replaceFirst(
"_"
));
}
}
|
既然正则表达式总是和字符串有关, Java 1.4对String类进行了扩展,提供了一个matches方法来匹配pattern。在方法内部使用Pattern和Matcher类来处理这些东西,但显然这样减少了代码的行数.
Pattern类同样有matches方法,可以让正则和作为参数输入的字符串匹配,输出布尔值结果.
下述的代码可以将输入字符串和正则表达式进行匹配.
1
2
3
|
String str =
"bbb"
;
System.out.println(
"Using String matches method: "
+str.matches(
".bb"
));
System.out.println(
"Using Pattern matches method: "
+Pattern.matches(
".bb"
, str));
|
所以如果你的需要仅仅是检查输入字符串是否和pattern匹配,你可以通过调用String的matches方法省下时间。只有当你需要操作输入字符串或者重用pattern的时候,你才需要使用Pattern和Matches类.
注意由正则定义的pattern是从左至右应用的,一旦一个原字符在一次匹配中使用过了,将不会再次使用.
例如,正则“121”只会匹配两次字符串“31212142121″,就像这样“_121____121″。 正则表达式通用匹配符号 。
Java正则表达式元字符 。
有两种方法可以在正则表达式中像一般字符一样使用元字符.
在元字符前添加反斜杠(\) 将元字符置于\Q(开始引用)和\E(结束引用)间 正则表达式量词 。
量词指定了字符匹配的发生次数.
量词可以和character classes和capturing group一起使用.
例如,[abc]+表示a,b或c出现一次或者多次.
(abc)+表示capturing group “abc”出现一次或多次。我们即将讨论capturing group.
正则表达式capturing group 。
Capturing group是用来对付作为一个整体出现的多个字符。你可以通过使用()来建立一个group。输入字符串中和capturing group相匹配的部分将保存在内存里,并且可以通过使用Backreference调用.
你可以使用matcher.groupCount方法来获得一个正则pattern中capturing groups的数目。例如((a)(bc))包含3个capturing groups; ((a)(bc)), (a) 和 (bc).
你可以使用在正则表达式中使用Backreference,一个反斜杠(\)接要调用的group号码.
Capturing groups和Backreferences可能很令人困惑,所以我们通过一个例子来理解.
1
2
3
4
|
System.out.println(Pattern.matches(
"(\\w\\d)\\1"
,
"a2a2"
));
//true
System.out.println(Pattern.matches(
"(\\w\\d)\\1"
,
"a2b2"
));
//false
System.out.println(Pattern.matches(
"(AB)(B\\d)\\2\\1"
,
"ABB2B2AB"
));
//true
System.out.println(Pattern.matches(
"(AB)(B\\d)\\2\\1"
,
"ABB2B3AB"
));
//false
|
在第一个例子里,运行的时候第一个capturing group是(\w\d),在和输入字符串“a2a2″匹配的时候获取“a2″并保存到内存里。因此\1是”a2”的引用,并且返回true。基于相同的原因,第二行代码打印false.
试着自己理解第三行和第四行代码。:) 。
现在我们来看看Pattern和Matcher类中一些重要的方法.
我们可以创建一个带有标志的Pattern对象。例如Pattern.CASE_INSENSITIVE可以进行大小写不敏感的匹配。Pattern类同样提供了和String类相似的split(String) 方法 。
Pattern类toString()方法返回被编译成这个pattern的正则表达式字符串.
Matcher类有start()和end()索引方法,他们可以显示从输入字符串中匹配到的准确位置.
Matcher类同样提供了字符串操作方法replaceAll(String replacement)和replaceFirst(String replacement).
现在我们在一个简单的java类中看看这些函数是怎么用的.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
package
com.journaldev.util;
import
java.util.regex.Matcher;
import
java.util.regex.Pattern;
public
class
RegexExamples {
public
static
void
main(String[] args) {
// using pattern with flags
Pattern pattern = Pattern.compile(
"ab"
, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(
"ABcabdAb"
);
// using Matcher find(), group(), start() and end() methods
while
(matcher.find()) {
System.out.println(
"Found the text \""
+ matcher.group()
+
"\" starting at "
+ matcher.start()
+
" index and ending at index "
+ matcher.end());
}
// using Pattern split() method
pattern = Pattern.compile(
"\\W"
);
String[] words = pattern.split(
"one@two#three:four$five"
);
for
(String s : words) {
System.out.println(
"Split using Pattern.split(): "
+ s);
}
// using Matcher.replaceFirst() and replaceAll() methods
pattern = Pattern.compile(
"1*2"
);
matcher = pattern.matcher(
"11234512678"
);
System.out.println(
"Using replaceAll: "
+ matcher.replaceAll(
"_"
));
System.out.println(
"Using replaceFirst: "
+ matcher.replaceFirst(
"_"
));
}
}
|
上述程序的输出:
1
2
3
4
5
6
7
8
9
10
|
Found the text
"AB"
starting at
0
index and ending at index
2
Found the text
"ab"
starting at
3
index and ending at index
5
Found the text
"Ab"
starting at
6
index and ending at index
8
Split using Pattern.split(): one
Split using Pattern.split(): two
Split using Pattern.split(): three
Split using Pattern.split(): four
Split using Pattern.split(): five
Using replaceAll: _345_678
Using replaceFirst: _34512678
|
这是不是一个很全面的Java正则表达式学习教程,希望对大家的学习有所帮助.
最后此篇关于Java正则表达式学习教程的文章就讲到这里了,如果你想了解更多关于Java正则表达式学习教程的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在做一个关于代码学院的教程,我在这里收到一个错误,说“看起来你的函数没有返回‘唉,你没有资格获得信用卡。资本主义就是这样残酷。’”当收入参数为 75 时。”但是该字符串在控制台中返回(由于某种原因
我正在阅读 Go 的官方教程,但很难理解 Channel 和 Buffered Channels 之间的区别。教程的链接是 https://tour.golang.org/concurrency/2和
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 9年前关闭。 Improve this que
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
作为 iOS 新手,有大量书籍可以满足学习基础知识的需求。现在,我想转向一些高级阅读,例如 OAuth 和 SQLite 以及动态 API 派生的 TableView 等。您可以推荐任何资源吗? 最佳
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 8 年前。 Improve
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 8 年前。
前言 很多同学都知道,我们常见的CTF赛事除了解题赛之外,还有一种赛制叫AWD赛制。在这种赛制下,我们战队会拿到一个或多个服务器。服务器的连接方式通常是SSH链接,并且可能一个战队可能会同时有
Memcached是一个自由开源的,高性能,分布式内存键值对缓存系统 Memcached 是一种基于内存的key-value存储,用来存储小块的任意数据(字符串、对象),这些数据可以是数据库调用、A
Perl 又名实用报表提取语言, 是 Practical Extraction and Report Language 的缩写 Perl 是由 拉里·沃尔(Larry Wall)于19
WSDL 是 Web Services Description Language 的缩写,翻译成中文就是网络服务描述语言 WSDL 是一门基于 XML 的语言,用于描述 Web Services 以
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
我正在寻找解释在 WPF 中创建自定义用户控件的教程。 我想要一个控件,它结合了一个文本 block 、一个文本框和一个启动通用文件打开对话框的按钮。我已经完成了布局,一切都连接好了。它有效,但它是三
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我接近 fourth page of the Django tutorial 的开始看着vote查看,最后是这样的: # Always return an HttpResponseRedirect a
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
是否有任何好的 Qt QSS 教程,或者在某个地方我可以看到样式小部件的示例?如果某处可用,我想要一些完整的引用。除了有关如何设置按钮或某些选项卡样式的小教程外,我找不到任何其他内容。 最佳答案 Qt
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我是一名优秀的程序员,十分优秀!