Sometimes we wish to know what exactly was the sequence of characters in the searched string that matched the regular expression.
If we enclose part of the regular expression with parentheses, the sequence of characters in the searched string that matched that part will be automatically assigned into a variable named $1.
If we enclose several parts of the regular expression with parentheses, their matched substrings in the target string will be sequentially assigned to $1, $2, $3 etc.
Let us now extract from the entry the day, month, year, hour and minutes.
#!/usr/local/bin/perl print "Please enter date and time, as in \"08-OCT-1997 16:30\"\n"; my $entry = <STDIN>; chop ($entry); $entry =~ /(\d\d)-(\w\w\w)-(\d\d\d\d) (\d\d):(\d\d)/; # $1 now contains the day # $2 contains the month; # $3 contains the year; # $4 contains the hour; # $5 contains the minutes; # for example, to print the month we would write: print "Month: $2\n";Note: as will be shown later, you can directly assign the substrings matching the parts in parentheses into variables named $day, $month etc.
The link:
List of Lecture Slides
The HTML source:
List of <A HREF="index.html">Lecture Slides</A>
The Perl program:
#!/usr/local/bin/perl my $html = "List of <A HREF=\"index.html\">Lecture Slides</A>."; $html =~ /<A HREF="(.*)">/; print "URL: $1\n";Result:
URL: index.html