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

Chapter 3

Matching Single Characters

The simplest regular expression involves matching a single character. If you want to match a single, specified alphabetic character or numeric digit, you simply use a pattern that consists of that character or digit. So, for example, to match the uppercase letter L, you would use the following pattern:

L

The pattern matches any occurrence of the uppercase L. You have not qualified the pattern in any way to limit matching, so expect it to match any occurrence of uppercase L. Of course, if matching is being carried out in a case-insensitive manner (which is discussed in Chapter 4), both uppercase L and lowercase l will be matched.

Try It Out

Matching a Single Character

You can apply this pattern to the sample document UpperL.txt, which is shown here:

Excel had XLM macros. They were replaced by Visual Basic for Applications in later versions of the spreadsheet software.

CMLIII

Leoni could swim like a fish.

Legal difficulties plagued the Clinton administration. Lewinski was the source of some of the former president’s difficulties.

1.Open OpenOffice.org Writer, and open the file UpperL.txt.

2.Use the Ctrl+F keyboard shortcut to open the Find and Replace dialog box, and check the Regular Expressions check box and the Match Case check box in the Options section.

3.Enter the regular expression pattern L in the Search For text box at the top of the Find and Replace dialog box, and click the Find All button.

If all has gone well, each occurrence of an uppercase L should be highlighted.

Figure 3-1 shows the matching of the pattern L in OpenOffice.org Writer against the sample document UpperL.txt. Notice that there are five matches contained in the sequences of characters XLM, CMLIII,

Leoni, Legal, and Lewinski.

How It Works

The default behavior of OpenOffice.org Writer is to carry out a case-insensitive match. As you can see in Figure 3-1, I have checked the Match Case check box so that only the same case as specified in the regular expression is matched.

42

Simple Regular Expressions

Figure 3-1

For each character in the document, OpenOffice.org Writer checks whether that character is an uppercase L. If it is, the regular expression pattern is matched. In OpenOffice.org Writer, a match is indicated by highlighting of the character(s) — in this case, a single character — for each match, assuming that the Find All button has been clicked.

How can you match a single character using JavaScript?

Try It Out

Matching a Single Character in JavaScript

You want to find all occurrences of uppercase L. You can express the simple task that you want to use regular expressions to do as follows:

Match any occurrence of uppercase L.

43

Chapter 3

You can see, using JavaScript as a sample technology, how most regular expression engines will match the pattern L using the XHTML file UpperL.html, shown here:

<html>

<head>

<title>Check for Upper Case L</title>

<script language=”javascript” type=”text/javascript”> var myRegExp = /L/;

function Validate(entry){ return myRegExp.test(entry); } // end function Validate()

function ShowPrompt(){

var entry = prompt(“This script tests for matches for the regular expression pattern: “ + myRegExp + “.\nType in a string and click on the OK button.”, “Type your text here.”);

if (Validate(entry)){

alert(“There is a match!\nThe regular expression pattern is: “ + myRegExp + “.\n The string that you entered was: ‘“ + entry + “‘.”);

}// end if else{

alert(“There is no match in the string you entered.\n” + “The regular expression pattern is “ + myRegExp + “\n” + “You entered the string: ‘“ + entry + “‘.” );

}// end else

}// end function ShowPrompt()

</script>

</head>

<body>

<form name=”myForm”> <br />

<button type=”Button” onclick=”ShowPrompt()”>Click here to enter text.</button> </form>

</body>

</html>

1.Navigate in Windows Explorer to the directory that contains the file UpperL.html, and doubleclick the file. It should open in your default browser.

2.Click the button labeled Click Here to Enter Text. A prompt window is shown, as you can see in Figure 3-2.

3.Type a character or a string in the text box that contains the default text Type your text here, and the JavaScript code will test whether or not there is a match for the regular expression pattern, in this case L. Click the OK button.

44

Simple Regular Expressions

Figure 3-2

4.Inspect the alert box that is displayed to assess whether or not a match is present in the string that you entered. Figure 3-3 shows the message when a successful match is made. Figure 3-4 shows the message displayed when the string that you enter does not match the regular expression pattern.

Figure 3-3

Figure 3-4

45

Chapter 3

How It Works

The simple Web page contains JavaScript code.

The JavaScript variable myRegExp is assigned the literal regular expression pattern L, using the following declaration and assignment statement:

var myRegExp = /L/;

In JavaScript, the forward slash is used to delimit a regular expression pattern in a way similar to how paired quotes are used to delimit a string. There is an alternate syntax, which is not discussed here.

When you click the button labeled Click Here to Enter Text, the ShowPrompt() function is called.

The entry variable is used to collect the string you enter in the prompt box:

var entry = prompt(“This script tests for matches for the regular expression pattern: “ + myRegExp + “.\nType in a string and click on the OK button.”, “Type your text here.”);

The output created depends on whether or not the text you entered contains a match for the regular expression pattern. Once the text has been entered and the OK button clicked, an if statement is executed, which checks whether or not the text you entered (and which is stored in the entry variable) contains a match for the regular expression pattern stored in the variable myRegExp:

if (Validate(entry)){

The if statement causes the Validate function to be called:

function Validate(entry){ return myRegExp.test(entry); } // end function Validate()

The test() method of the myRegExp variable is used to determine whether or not a match is present.

If the if statement:

if (Validate(entry))

returns the Boolean value true, the following code is executed

alert(“There is a match!\nThe regular expression pattern is: “ + myRegExp + “.\n

The string that you entered was: ‘“ + entry + “‘.”);

and uses the myRegExp and entry variables to display the regular expression pattern and the string that you entered, together with explanatory text.

If there is no match, the following code is executed, because it is contained in the else clause of the if statement:

alert(“There is no match in the string you entered.\n” + “The regular expression

pattern is “ + myRegExp + “\n” + “You entered the string: ‘“ + entry + “‘.” );

46