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

Chapter 4

Escaped Characters

Some characters, such as the . character, must be escaped if you want to use them to match the corresponding literal character. You saw earlier in the chapter the use of the \. metasequence to match a period.

Another character that requires special treatment is the backslash.

Finding the Backslash

You have seen that the backslash (\) is frequently used as part of a pair of characters that escape a character that would otherwise be interpreted as a metacharacter in a regular expression pattern, or the backslash is used together with another character to form a character pair, which is interpreted as a metacharacter inside a regular expression pattern. The question, therefore, arises as to how you select the backslash character literally.

Suppose that you have a document that contains many URLs. A URL should contain only forward slash characters (/) and never any backslash characters. The sample document URLS.txt contains several URLs, some of which correctly use forward slash characters and some of which have residual undesired backslash characters:

http://www.w3.org/

http://www.amazon.com/

http://www.w3.org\tr/

http://www.XMML.com\default.svg

http://www.wiley.com/

http:\\www.wrox.com/

http://www.example.org/

The problem definition can be expressed like this:

Find any backslash character in the file URLS.txt.

Assume that all the URLs that you are interested in begin with the four-character sequence http. What you need to find is any occurrence of the backslash character following that initial sequence of characters.

The following regular expression pattern finds all occurrences of the initial four-character sequence followed by zero or more characters, indicated by the pattern .*, followed by a single backslash, indicated by the pattern \\ , followed by zero or more characters, indicated by the pattern .*.

http.*\\.*

Figure 4-21 shows the result of using that pattern in searching the sample file URLS.txt in OpenOffice.org Writer.

102