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

C# and Regular Expressions

The regular expression to be matched is in the second argument of the Replace() method, (\w+)\s+(\1). That pattern matches a sequence of word characters equivalent to the character class [A-Za-z0-9_] followed by one or more whitespace characters and, as indicated by the \1 back reference, the same sequence of word characters that has already been matched. In other words, the pattern matches a doubled word separated by whitespace.

The third argument of the Replace() method is the pattern to be used to replace any matched text. The matched text contains the doubled word (if one exists). The replacement text uses the numbered group corresponding to the back reference, ${1}, to replace two occurrences of the word with one:

string outputString = Regex.Replace(inputString, @”(\w+)\s+(\1)”,

“${1}”);

Then the original string and the changed string are displayed to the user:

Console.WriteLine(“You entered the string: ‘“ + inputString + “‘.”);

Console.WriteLine(“The replaced string is ‘“ + outputString + “‘.”);

Console.ReadLine();

Exercise

1.Which of the RegexOptions is used to specify case-insensitive matching?

547

23

PHP and Regular

Expressions

PHP, the PHP Hypertext Processor, is a widely used language for Web-based applications. One common task in Web-based applications, whatever language is used, is the validation of user input either on the client side or on the server side before data is written to a relational database.

PHP is typically used on the server side and has similarities to ASP and ASP.NET. To work through the examples in this chapter, you will need to install PHP on a Web server.

In this chapter, you will learn the following:

How to get started with PHP 5.0

How PHP structures support for regular expressions

How to use the ereg() family of functions

What metacharacters are supported in PHP in Perl Compatible Regular Expressions (PCRE)

How to match commonly needed user entries

This chapter describes the regular expression functionality in PHP version 5.0.

Getting Star ted with PHP 5.0

To run the examples shown in this chapter, you must install PHP on a Web server. Because this book is focusing on the use of regular expressions on the Windows platforms, the focus will be on installing PHP on a Windows IIS server.

With the advent of PHP 5.0, the recommended methods of installing PHP have changed significantly from those previously recommended on www.php.net.

Chapter 23

The PHP Web site at www.php.net is the official source of up-to-date information about PHP. This chapter focuses on PHP 5.0 functionality, but it is possible that recommendations on installation and/or configuration will change. I suggest that you check the URL given for the current situation.

If you need PHP 4 rather than PHP 5, that can still be downloaded from www.php.net/ downloads.php at the time of this writing. If, for compatibility reasons, you need PHP 3, it can be downloaded from http://museum.php.net/.

The following instructions describe how to install PHP 5.0.1 using the Windows installer package. It is assumed that you have already installed IIS. The Windows installer package is the easiest way to install PHP on Windows, but it has limitations. The PHP installation files are also available as a .zip file, which has to be installed manually but does allow full control over how PHP is installed. Because the focus of this chapter is the use of regular expressions with PHP, rather than on a detailed consideration of PHP installation on a Web server, no information on installation and configuration of the .zip file download is provided here.

The following instructions should get you up and running. But be aware that they take no account of how to create a secure PHP installation. If you want to use PHP on a production server, be sure to invest time in fully understanding the security issues relating to the use of PHP on the Internet.

Try It Out

Installing PHP Using the Windows Installer

1.Download the Windows installer from the download page on www.php.net (at the time of this writing, downloads were listed on www.php.net/downloads.php), and double-click the Windows installer package. Figure 23-1 shows the initial screen of the installer package for PHP 5.0.1.

Figure 23-1

2.Click the Next button. Read the License Agreement, and click the I Agree button. If you don’t accept the license, you won’t be able to use the installer to install PHP 5.0.1.

550

PHP and Regular Expressions

3.On the next screen, you are offered a choice between Standard and Advanced installation. Select Advanced, and click the Next button.

4.Choose a location for installation. I chose C:\PHP 5.0.1. Click the Next button.

5.You are then asked if you want to create backups of any file replaced during installation. Leave the default option, Yes, and click the Next button.

6.Accept the default upload directory, and click the Next button. Accept the default directory for session information, and click the Next button.

7.Accept localhost as your SMTP server location or modify it as appropriate. For the purposes of this test installation, I suggest that you accept localhost; then click the Next button.

8.Accept the default option about warnings and errors, and click the Next button.

9.On the next screen, the installer is likely to recognize the version of IIS or Personal Web Server (PWS) you have installed. Unless you have good reason to do otherwise, accept the default, and click the Next button.

10.Select the file extensions to be associated with PHP. I suggest that you restrict this to .php unless you have a specific need to do otherwise. Click the Next button.

11.On the next screen, you are informed that the installer has the needed information to carry out the installation. Click the Next button. The installer will display messages about progress of the installation. If all has gone well, you should see the message shown in Figure 23-2.

Figure 23-2

Now that PHP appears to have been installed successfully, you need to test whether it works correctly with IIS. The following instructions assume that IIS is installed, that it is running on the local machine, and that the default directory for IIS content is C:\inetpub\wwwroot\. If you have a different setup, amend the following instructions accordingly.

12.In Notepad or some other text editor, type the following code:

<?php

phpinfo()

?>

13.Create a subdirectory PHP in C:\inetpub\wwwroot\ or an alternative location, if you prefer. That will allow you to access PHP code using the URL http://localhost/PHP/ plus the relevant PHP filename.

551

Chapter 23

14.Save the file as phpinfo.php in the PHP directory. If you are saving from Notepad, be sure to enclose the filename in paired double quotes, or Notepad will save the file as phpinfo.php.txt, which won’t run correctly when accessed from a Web browser.

15.Open Internet Explorer or an alternative browser, and type the URL http://localhost/ PHP/phpinfo.php into the browser.

Figure 23-3 shows the result you should expect to see after this step. Naturally, if you are not using Internet Explorer 6.0 or PHP 5.0.1, the appearance will differ a little from that shown. However, if you see a Web page similar to that in Figure 23-3, you have a successful install of PHP.

Figure 23-3

16.Use the Ctrl+F keyboard shortcut to search for the text PCRE in the Web page. As shown in Figure 23-4, the PCRE functionality is enabled by default in PHP 5.0.1 installed using the Windows installer option. Because some of the examples in this chapter depend on the presence of PCRE functionality, it is important that you verify that it is enabled.

Now that you know your PHP installation is working, you can move on to take a closer look at the ways in which regular expression functionality is supported in PHP 5.0.

552