Page 122 - Open Soource Technologies 304.indd
P. 122

Web Technologies-I



                   Notes                Example:

                                 // prints “Found ‘cat’”
                                 if (ereg(“cat”, “raining cats and dogs”))
                                     print “Found ‘cat’”;
                                 The regular expression cat matches the subject string, and the fragment prints “Found ‘cat’”.
                                 Characters and Wildcards

                                 To represent any character in a pattern, a period is used as a wildcard. The pattern c. matches
                                 any three-letter string that begins with a lowercase c; for example, cat, cow, cop, and so on. To
                                 express a pattern that actually matches a period, use the backslash character \. For example, .com
                                 matches both .com and xcom but \.com matches’ only .com.
                                 The use of the backslash in a regular expression can cause confusion. To include a backslash in
                                 a double-quoted string, you need to escape the meaning of the backslash with a backslash. The
                                 following example shows how the regular expression pattern “\.com” is represented:
                                        Example:

                                 // Sets $found to true
                                 $found = ereg(“\\.com”, “www.ora.com”);
                                 It is better to avoid the confusion and use single quotes when passing a string as a regular
                                 expression:

                                 $found = ereg(‘\.com’, “www.ora.com”);
                                 Character lists
                                 Rather than using a wildcard that matches any character, a list of characters enclosed in brackets can
                                 be specified within a pattern. For example, to match a three-character string that starts with a “p”,
                                 ends with a “p”, and contains a vowel as the middle letter, you can use the following expression:
                                 ereg(“p[aeiou]p”, $var)

                                 This returns true for any string that contains “pap”, “pep”, “pip”, “pop”, or “pup”. The character
                                 list in the regular expression “p[aeiou]p” matches with exactly one character, so strings like
                                 “paep” do not match. A range of characters can also be specified; for example, “[0-9]” specifies
                                 the numbers 0 through 9:

                                        Example:
                                 // Matches “A1”, “A2”, “A3”, “B1”, ...

                                 $found = ereg(“[ABC][123]”, “A1 Quality”);  // true
                                 // Matches “00” to “39”
                                 $found = ereg(“[0-3][0-9]”, “27”);  //true
                                 $found = ereg(“[0-3][0-9]”, “42”);  //false
                                 A list can specify characters that are not matches using the not operator ^ as the first character
                                 in the brackets. The pattern “[^123]” matches any character other than 1, 2, or 3. The following
                                 examples show regular expressions that make use of the not operator in lists:

                                        Example:

                                 // true for “pap”, “pbp”, “pcp”, etc. but not “php”
                                 $found = ereg(“p[^h]p”, “pap”); //true


        116                               LOVELY PROFESSIONAL UNIVERSITY
   117   118   119   120   121   122   123   124   125   126   127