Blame


1 ffd9a51f 2023-08-04 jrmu #!/usr/bin/perl
2 ffd9a51f 2023-08-04 jrmu
3 ffd9a51f 2023-08-04 jrmu # Extra credit exercise: modify the program from the previous exercise so
4 ffd9a51f 2023-08-04 jrmu # that immediately following the word ending in 'a' it will also capture
5 ffd9a51f 2023-08-04 jrmu # up-to-five characters (if there are that many characters, of course) in a
6 ffd9a51f 2023-08-04 jrmu # separate capture variable. Update the code to display both capture
7 ffd9a51f 2023-08-04 jrmu # variables. For example, if the input string says 'I saw Wilma yesterday',
8 ffd9a51f 2023-08-04 jrmu # the up-to-five characters are " yest" (with the leading space). If the
9 ffd9a51f 2023-08-04 jrmu # input is "I, Wilma!", the extra capture should have just one character.
10 ffd9a51f 2023-08-04 jrmu # Does your pattern still match just plain 'wilma'?
11 ffd9a51f 2023-08-04 jrmu
12 ffd9a51f 2023-08-04 jrmu use warnings;
13 ffd9a51f 2023-08-04 jrmu use strict;
14 ffd9a51f 2023-08-04 jrmu use utf8;
15 ffd9a51f 2023-08-04 jrmu
16 ffd9a51f 2023-08-04 jrmu while (<>) {
17 ffd9a51f 2023-08-04 jrmu chomp();
18 ffd9a51f 2023-08-04 jrmu if (/\b(?<word>\w*a)\b(?<trail>.{0,5})/) {
19 ffd9a51f 2023-08-04 jrmu print "Matched: $`<$&>$': word contains '$+{word}', trailed by '$+{trail}'\n";
20 ffd9a51f 2023-08-04 jrmu } else {
21 ffd9a51f 2023-08-04 jrmu print "No match: |$_|\n";
22 ffd9a51f 2023-08-04 jrmu }
23 ffd9a51f 2023-08-04 jrmu }