Documentation

The Java™ Tutorials
Hide TOC
Questions and Exercises问题和练习
Trail: Essential Java Classes
Lesson: Regular Expressions

Questions and Exercises: Regular Expressions问题和练习:正则表达式

Questions

  1. What are the three public classes in the java.util.regex package? java.util.regex包中的三个公共类是什么?Describe the purpose of each.请描述每种方法的目的。
  2. Consider the string literal "foo". 考虑字符串文字"foo"What is the start index? 起始索引是什么?What is the end index? 末端索引是什么?Explain what these numbers mean.解释这些数字的含义。
  3. What is the difference between an ordinary character and a metacharacter? 普通字符和元字符之间有什么区别?Give an example of each.举一个例子。
  4. How do you force a metacharacter to act like an ordinary character?如何强制元字符像普通字符一样工作?
  5. What do you call a set of characters enclosed in square brackets? What is it for?用方括号括起来的一组字符叫什么?这是干什么用的?
  6. Here are three predefined character classes: \d, \s, and \w. 以下是三个预定义的字符类:\d\s\wDescribe each one, and rewrite it using square brackets.描述每一个,并用方括号重写。
  7. For each of \d, \s, and \w, write two simple expressions that match the opposite set of characters.对于\d\s\w中的每一个,编写两个与相反字符集匹配的简单表达式。
  8. Consider the regular expression (dog){3}. 考虑正则表达式(dog){3}Identify the two subexpressions. 识别这两个子表达式。What string does the expression match?表达式与哪个字符串匹配?

Exercises练习

  1. 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.检查你的答案。


Previous page: Additional Resources
Next page: End of Trail