jmcnamara has asked for the wisdom of the Perl Monks concerning the following question:
The task is to extract double quoted strings from another string. For the string
'"foo" & "bar"'
the matched strings would be "foo" and "bar" including the quotes.
The thing that makes this a little tricky is that double quotes in the quoted string are escaped using a pair of quotes. I think this escape format comes from Basic. Thus, the string " He said "maybe" " would be escaped as follows:
'" He said ""maybe"" "'
This leads to some nice pathological cases such as '""""""""""' and '" """&" """'
Here is a test framework if you care to bend your brain to this puzzle. I'll post my own answer later.
Problem. Write a function find_quote() which takes a string and adds <> around the double quoted strings.
For example
'"foo" & "bar"' -> '<"foo"> & <"bar">'
Double quotes within the string are escaped as a pair of double quotes. The quotes will always be balanced. Apart from double quotes there can be any arbitrary characters before or after the strings. The test below should demonstrate the types of string to expect.
#!/usr/bin/perl -w use strict; use Test::More 'no_plan'; # Test input and target output my %strings = ( '' => '', '""' => '<"">', '""""' => '<"""">', '""&""&""&""&""' => '<"">&<"">&<"">&<"">&<"">', '""""""""""' => '<"""""""""">', '""""&""""' => '<"""">&<"""">', '"""&"""' => '<"""&""">', '"foo"&"bar"' => '<"foo">&<"bar">', ' "foo" & "bar" ' => ' <"foo"> & <"bar"> ', '" "' => '<" ">', '" "" "' => '<" "" ">', '" """&" """' => '<" """>&<" """>' , '" "" "' => '<" "" ">', '" "" "&" "" "' => '<" "" ">&<" "" ">', '" "" & "" "' => '<" "" & "" ">', '""&""""' => '<"">&<"""">', ' "" ' => ' <""> ', ' """" ' => ' <""""> ', ' ""&""&""&""&"" ' => ' <"">&<"">&<"">&<"">&<""> + ', ' """""""""" ' => ' <""""""""""> ', 'test("foo","bar")' => 'test(<"foo">,<"bar">)', ); # Add your code here sub find_quote { my $str = $_[0]; # Broken example $str =~ s/(".*?")/<$1>/g; return $str; } # Run the tests while (my($input, $result) = each %strings) { is(find_quote($input), $result, "for string:\t" . "'$input'"); }
--
John.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
•Re: Matching escaped quoted strings
by merlyn (Sage) on Feb 19, 2004 at 17:43 UTC | |
|
Re: Matching escaped quoted strings
by Abigail-II (Bishop) on Feb 19, 2004 at 17:54 UTC | |
by ysth (Canon) on Feb 19, 2004 at 18:31 UTC | |
|
Re: Matching escaped quoted strings
by jmcnamara (Monsignor) on Feb 19, 2004 at 21:15 UTC |