The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。
See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Questions and Exercises: Regular Expressions问题和练习:正则表达式
Questions
What are the three public classes in the java.util.regex
package? java.util.regex
包中的三个公共类是什么?Describe the purpose of each.请描述每种方法的目的。
Consider the string literal "foo"
. 考虑字符串文字"foo"
。What is the start index? 起始索引是什么?What is the end index? 末端索引是什么?Explain what these numbers mean.解释这些数字的含义。
What is the difference between an ordinary character and a metacharacter? 普通字符和元字符之间有什么区别?Give an example of each.举一个例子。
How do you force a metacharacter to act like an ordinary character?如何强制元字符像普通字符一样工作?
What do you call a set of characters enclosed in square brackets? What is it for?用方括号括起来的一组字符叫什么?这是干什么用的?
Here are three predefined character classes: \d
, \s
, and \w
. 以下是三个预定义的字符类:\d
、\s
和\w
。Describe each one, and rewrite it using square brackets.描述每一个,并用方括号重写。
For each of \d
, \s
, and \w
, write two simple expressions that match the opposite set of characters.对于\d
、\s
和\w
中的每一个,编写两个与相反字符集匹配的简单表达式。
Consider the regular expression (dog){3}
. 考虑正则表达式(dog){3}
。Identify the two subexpressions. 识别这两个子表达式。What string does the expression match?表达式与哪个字符串匹配?
Exercises练习
Use a backreference to write an expression that will match a person's name only if that person's first name and last name are the same.仅当某人的名字和姓氏相同时,使用反向引用编写与该人姓名匹配的表达式。
Check your answers.检查你的答案。