CS1655 Recitation 2 - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

CS1655 Recitation 2

Description:

Test if a string or its substring matches with some pattern. ... negated character class Match any character not listed t tab Match HT or TAB character. ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 16
Provided by: Am69
Category:

less

Transcript and Presenter's Notes

Title: CS1655 Recitation 2


1
CS1655 Recitation 2
  • Andreea Munteanu
  • Andreea_at_cs.pitt.edu

2
Overview
  • Perl regular expressions.
  • Wget.
  • Examples.

3
Perl regular expression (1)
  • Extremely Powerful Text Processing.
  • Test if a string or its substring matches with
    some pattern.
  • Replace or substitute some string pattern in a
    text string.
  • Extract substring from a string based on certain
    text pattern.

4
Perl regular expression (2)
  • Comparison with a string
  • if (var /BLAH/) Match string.
  • if (var /BLAH/) Start of String.
  • if (var /BLAH/) End of String.
  • if (var /\w/) Any letters.

5
Perl regular expression (3)
  • Items that match  a single character in a
    predefined character class
  • \w  Match a "word" character.
  • (alphanumeric  plus "_").
  • \W  Match a non-word character.
  • \s  Match a whitespace character.
  • \S  Match a non-whitespace character.
  • \d  Match a digit character.
  • \D  Match a non-digit character.

6
Perl regular expression (4)
  • Items to match a single character
  • . dot Match any one
    characters.
  • ... character class Match any
    character listed.
  • ... negated character class Match any
    character not listed
  • \t tab Match HT or TAB character.
  • \n new line Match LF or NL character.
  • \r return Match CR character.

7
Perl regular expression (5)
  • Quantity symbols
  • Match 0 or more times.
  • Match 1 or more times.
  • ? Match 0 or 1 times.
  • n Match exactly n times.
  • n, Match at least n times.
  • n,m Match at least n times, but no more
    than m times.

8
Perl regular expression (6)
  • Metacharacters
  • These need to be escaped to be matched
  • \ . ? ( )

9
Examples
  • Example 1 (Character class)
  • if(string /010-9/)
  • print string contains digits 00 to 19\n"
  • elseprint "string does not contain digits 00 to
    19\n"

10
Perl regular expression substitution
  • Substitution is specified with s.
  • string s/apples/oranges/
  • Wherever "apples" is found in string, it will be
    replaced by
  • "oranges". Note it replaces only the first
    appearance of the
  • searched string.
  • To replace all appearances, use a g at the end
  • string s/apples/oranges/g
  • To ignore case-sensitivity, use i at the end
  • string s/apples/oranges/gi

11
Perl substring extraction
  • Example problem. Given the path
  • c/anypath1/Apath2/Bpath3/code01038_18e.jpg
  • we want to extract "code01038_18e" and then to
    modify it to "code01038_18a".

12
Example solution
  • !/usr/bin/perl
  • source "c/anypath1/Apath2/Bpath3/code01038_18
    e.jpg"
  • print "source\n" Extract everything from the
    last / onward
  • ind rindex(source, "/")
  • tmp1 substr(source, ind 1, -4)
  • Remove the last character and replace with 'a'
  • print "tmp1\n"
  • tmp2 substr(tmp1, 0, -1) . 'a'
  • print "tmp2\n"

13
Wget
  • A network utility to retrieve files from the Web
    using http and ftp protocols.
  • Works non-interactively, in the background, even
    after having logged off.
  • Works well with slow or unstable connections by
    continuing to retrieve a document until the
    document is fully downloaded.

14
Examples Wget (1)
  • Download a url gt just type
  • wget http//fly.srk.fer.hr/
  • The connection will probably fail before the
    whole file is retrieved gt retry a number of
    times
  • wget --tries45 http//fly.srk.fer.hr/jpg/flyweb.
    jpg

15
Examples Wget (2)
  • Leave Wget to work in the background, and write
    its progress to log file log
  • wget -t 45 -o log http//fly.srk.fer.hr/jpg/flywe
    b.jpg
  • If you specify a directory gt retrieve the
    directory listing, parse it and convert it to
    html. Try
  • wget ftp//ftp.gnu.org/pub/gnu/ links
    index.html
  • To unlimit the number of retries -t inf.
Write a Comment
User Comments (0)
About PowerShow.com