Fundamentals of Web Programming Lecture 10 ServerSide Scripting
Fundamentals of Web Programming Lecture 10: Server-Side Scripting II 20 -753: Fundamentals of Web Programming Lecture 10: Server-Side Scripting II 1
Announcements • Your Feedback is Welcome – Feel free to send email with your comments, questions, suggestions, etc. on the course so far – Some students have expressed a need for more fundamentals on programming and operating systems 20 -753: Fundamentals of Web Programming Lecture 10: Server-Side Scripting II 2
Announcements • Upcoming Recitations – After class (3 pm-4 pm) – Optional (intended for those with less programming experience) – Format: informal lectures & hands-on practice – Topics • Fri 6/4: Programming Basics • Mon 6/7: Operating Systems Basics 20 -753: Fundamentals of Web Programming Lecture 10: Server-Side Scripting II 3
Today’s Topics • Regular Expressions in PERL • String Operations: – Pattern Matching – Substitution • Examples of Pattern Matching • Putting It Together: An Automated Link Tester 20 -753: Fundamentals of Web Programming Lecture 10: Server-Side Scripting II 4
Regular Expressions • Chapter 7 in ‘Learning Perl’ • A pattern to be matched against a string • Match may assign values to match variables • Match may be used to replace parts of a string (substitution) 20 -753: Fundamentals of Web Programming Lecture 10: Server-Side Scripting II 5
Regular Expressions • Used by many programs (Unix commands, shells, etc. ) • Useful for processing text files • E. g. , web pages – matching links – matching particular tags, tag contents – etc. 20 -753: Fundamentals of Web Programming Lecture 10: Server-Side Scripting II 6
Simple Examples • print if ($line =~ /abc/); # exact match • print if ($line =~ /ab*c/); # zero or more b’s 20 -753: Fundamentals of Web Programming Lecture 10: Server-Side Scripting II 7
Character Classes • [abcde] # match one of them • [a-z] # specify a range • [^a-z] # can be negated 20 -753: Fundamentals of Web Programming Lecture 10: Server-Side Scripting II 8
Predefined Classes • Element Equivalent d (digit) [0 -9] w (word char) [a-z. A-Z 0 -9_] s (space char) [ rtnf]. (any char) [^n] Negation D W S n • Classes can be combined; e. g. : [da-f. A-F] # one hex digit 20 -753: Fundamentals of Web Programming Lecture 10: Server-Side Scripting II 9
Multipliers • Operator * + ? • Examples: Meaning “zero or more” “one or more” “zero or one” – d*a? – [a-z. A-Z. ]+ 20 -753: Fundamentals of Web Programming Lecture 10: Server-Side Scripting II 10
Multipliers • General Multipliers – x{a, b} – x{5, } – x{0, 5} – x{5} # # between a and b x’s 5 or more x’s 0 to 5 x’s exactly 5 x’s 20 -753: Fundamentals of Web Programming Lecture 10: Server-Side Scripting II 11
Matches with Memory • Parts of a pattern match can be saved by using parenthesis to enclose parts of the pattern • Example: /a(b+)c/ • The result is stored in $1, $2, etc. (you can have >1 memory variable) 20 -753: Fundamentals of Web Programming Lecture 10: Server-Side Scripting II 12
Patterns are Greedy • Patterns match the maximum number of characters possible • Example: $line = “fred xxxxxx barney”; $line =~ /(x+)/; print “$1n”; • Output: xxxxxx 20 -753: Fundamentals of Web Programming Lecture 10: Server-Side Scripting II 13
Anchors • /^abc/ • /abc$/ • /abcb/ # start of line # end of line # word boundary 20 -753: Fundamentals of Web Programming Lecture 10: Server-Side Scripting II 14
Pattern Match Examples • Sample program which shows the results of various patterns: findpat. cgi 20 -753: Fundamentals of Web Programming Lecture 10: Server-Side Scripting II 15
Substitutions • Two regular expressions – what to match – what to substitute • Examples: – s/test/quiz/; – s|<b>(. *)</b>|$1|; 20 -753: Fundamentals of Web Programming Lecture 10: Server-Side Scripting II 16
Putting It Together • Automated Link Tester – Input: a URL – Output: an HTML page that lists all the links from that page – recursively follow links to subpages and search them, too – print “OK” if the link points to a page, “NOT FOUND” otherwise 20 -753: Fundamentals of Web Programming Lecture 10: Server-Side Scripting II 17
Example Program • testlinks. html 20 -753: Fundamentals of Web Programming Lecture 10: Server-Side Scripting II 18
- Slides: 18