27.07.2013 Views

Deitel - Python, How To Program.pdf

Deitel - Python, How To Program.pdf

Deitel - Python, How To Program.pdf

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

pythonhtp1_29.fm Page 1408 Friday, September 28, 2001 2:18 PM<br />

1408 PHP Chapter 29<br />

27<br />

28 // search for pattern 'Now' at the end of the string<br />

29 if ( ereg( "Now$", $search ) )<br />

30 print( "String 'Now' was found at the end<br />

31 of the line." );<br />

32<br />

33 // search for any word ending in 'ow'<br />

34 if ( ereg( "[[::]]", $search,<br />

35 $match ) )<br />

36 print( "Word found ending in 'ow': " .<br />

37 $match[ 1 ] . "" );<br />

38<br />

39 // search for any words beginning with 't'<br />

40 print( "Words beginning with 't' found: ");<br />

41<br />

42 while ( eregi( "[[::]]",<br />

43 $search, $match ) ) {<br />

44 print( $match[ 1 ] . " " );<br />

45<br />

46 // remove the first occurrence of a word beginning<br />

47 // with 't' to find other instances in the string<br />

48 $search = ereg_replace( $match[ 1 ], "", $search );<br />

49 }<br />

50<br />

51 print( "" );<br />

52 ?><br />

53 <br />

54 <br />

Fig. Fig. 29.8 29.8 Using regular expressions (part 2 of 2).<br />

We begin by assigning the string "Now is the time" to variable $search (line<br />

14). Line 19’s condition calls function ereg to search for the literal characters Now inside<br />

variable $search. If the pattern is found, ereg returns true, and line 20 prints a message<br />

indicating that the pattern was found. We use single quotes ('') inside the print<br />

statement to emphasize the search pattern. When located inside a string, content delimited<br />

by single quotes is interpolated. If a print statement uses only single quotes, the content<br />

inside the single quotes is not interpolated. For example,<br />

print( '$name' );<br />

© Copyright 1992–2002 by <strong>Deitel</strong> & Associates, Inc. All Rights Reserved. 9/28/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!