Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Regular Expressions 2005.pdf
Скачиваний:
95
Добавлен:
17.08.2013
Размер:
25.42 Mб
Скачать

Chapter 9

If you use the preceding pattern in matching that is case sensitive, there is no problem. The undesired character sequence star. does not match. However, if the matching is case insensitive, the undesired character sequence star. will match, lowering the specificity of the chosen pattern.

Figure 9-10

Exercises

Test your understanding of the material in this chapter using the following exercises:

1.Modify the pattern ^\w*(?<=\w)\.?\w+@(?=[\w\.]+\W)\w+\.\w{3,4}$, which was developed earlier in the chapter for matching email addresses, so that it matches only hostnames in the .com, .net, and .org domains.

2.Modify the pattern in the Star Training Company example to match the character sequence star so that it also matches in data like the following:

What do you think of Star?

The best training company is Star!

240

10

Documenting and Debugging

Regular Expressions

Because you are reading this book, you have in all likelihood already discovered that regular expressions can be hard to write. Regular expressions can also be hard to read, whether they were written by somebody else or by you, even a short time ago. When regular expressions are used in a project over a period of time, you come face to face with a third truism: Regular expressions are hard to maintain. The purpose of this chapter is to help you take steps to minimize the effects of these three truisms.

A basic consideration that is important not to forget is that regular expressions never occur in isolation. They are always used to work on data, whether simple or extensive, and are used in the context of a tool or a programming language. In addition, the developer has a specific purpose, sometimes a complex or subtle business purpose, for the regular expressions that he writes.

The problems that arise when using regular expressions can be due simply to being unable to write patterns that express the matching characteristics that you want. Ideally, as you work through this book, that problem will become less and less common.

In this chapter, you will learn the following:

How to document regular expressions

How to explore the data you are working with

How to create test cases for regular expressions

How to debug regular expressions