Presentation is loading. Please wait.

Presentation is loading. Please wait.

20-753: Fundamentals of Web Programming 1 Lecture 10: Server-Side Scripting II Fundamentals of Web Programming Lecture 10: Server-Side Scripting II.

Similar presentations


Presentation on theme: "20-753: Fundamentals of Web Programming 1 Lecture 10: Server-Side Scripting II Fundamentals of Web Programming Lecture 10: Server-Side Scripting II."— Presentation transcript:

1 20-753: Fundamentals of Web Programming 1 Lecture 10: Server-Side Scripting II Fundamentals of Web Programming Lecture 10: Server-Side Scripting II

2 20-753: Fundamentals of Web Programming 2 Lecture 10: Server-Side Scripting II 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

3 20-753: Fundamentals of Web Programming 3 Lecture 10: Server-Side Scripting II Announcements Upcoming Recitations –After class (3pm-4pm) –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

4 20-753: Fundamentals of Web Programming 4 Lecture 10: Server-Side Scripting II Today’s Topics Regular Expressions in PERL String Operations: –Pattern Matching –Substitution Examples of Pattern Matching Putting It Together: An Automated Link Tester

5 20-753: Fundamentals of Web Programming 5 Lecture 10: Server-Side Scripting II 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)

6 20-753: Fundamentals of Web Programming 6 Lecture 10: Server-Side Scripting II 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.

7 20-753: Fundamentals of Web Programming 7 Lecture 10: Server-Side Scripting II Simple Examples print if ($line =~ /abc/); # exact match print if ($line =~ /ab*c/); # zero or more b’s

8 20-753: Fundamentals of Web Programming 8 Lecture 10: Server-Side Scripting II Character Classes [abcde] # match one of them [a-z] # specify a range [^a-z] # can be negated

9 20-753: Fundamentals of Web Programming 9 Lecture 10: Server-Side Scripting II Predefined Classes ElementEquivalentNegation \d (digit)[0-9]\D \w (word char)[a-zA-Z0-9_]\W \s (space char)[ \r\t\n\f]\S. (any char)[^\n]\n Classes can be combined; e.g.: [\da-fA-F] # one hex digit

10 20-753: Fundamentals of Web Programming 10 Lecture 10: Server-Side Scripting II Multipliers OperatorMeaning *“zero or more” +“one or more” ?“zero or one” Examples: –\d*a? –[a-zA-Z\.]+

11 20-753: Fundamentals of Web Programming 11 Lecture 10: Server-Side Scripting II Multipliers General Multipliers –x{a,b}# between a and b x’s –x{5,} # 5 or more x’s –x{0,5}# 0 to 5 x’s –x{5}# exactly 5 x’s

12 20-753: Fundamentals of Web Programming 12 Lecture 10: Server-Side Scripting II 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)

13 20-753: Fundamentals of Web Programming 13 Lecture 10: Server-Side Scripting II Patterns are Greedy Patterns match the maximum number of characters possible Example: $line = “fred xxxxxx barney”; $line =~ /(x+)/; print “$1\n”; Output: xxxxxx

14 20-753: Fundamentals of Web Programming 14 Lecture 10: Server-Side Scripting II Anchors /^abc/# start of line /abc$/# end of line /abc\b/# word boundary

15 20-753: Fundamentals of Web Programming 15 Lecture 10: Server-Side Scripting II Pattern Match Examples Sample program which shows the results of various patterns: findpat.cgi findpat.cgi

16 20-753: Fundamentals of Web Programming 16 Lecture 10: Server-Side Scripting II Substitutions Two regular expressions –what to match –what to substitute Examples: –s/test/quiz/; –s| (.*) |$1|;

17 20-753: Fundamentals of Web Programming 17 Lecture 10: Server-Side Scripting II 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

18 20-753: Fundamentals of Web Programming 18 Lecture 10: Server-Side Scripting II Example Program testlinks.html


Download ppt "20-753: Fundamentals of Web Programming 1 Lecture 10: Server-Side Scripting II Fundamentals of Web Programming Lecture 10: Server-Side Scripting II."

Similar presentations


Ads by Google