Paul Beckett wrote:
>
> I'm trying to pattern match the beginning of a SQL string like:
> INSERT INTO `rwikiobject` VALUES
> ('0b5e02f308c5341d0108fca900670107','2006-03-06
> 23:36:41','/site/ec07580d-1c66-469f-80be-c0afd616cedf/alembert, d
> \'','/site/ec07580d-1c66-469f-80be-c0afd616cedf'
>
> My expression so far looks like:
> my ($id, $version, $name, $realm) = $object =~ /INSERT INTO
> `rwikiobject` VALUES \('([a-z0-9]{32})','(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:
> \d{2})','(\/site\/[^']+)','\/site\/([^']+)'/;
>
> This is working great, except for a few cases (like the example string
> above where the third field 'name' has an "escaped apostrophe": \'
>
> Do I need a reverse looking assertion to overcome this? If so, I would
> really appreciate a hand with the syntax of that. If not, any ideas how
> I can match this field: which is of unknown length, and can contain any
> character / symbol, including the escaped form of the apostrophe, which
> is also the field delimiter.

Hi Paul.

Those string values look exactly like Perl syntax to me, so I would forget about
regexes and treat it as Perl source. Imagine if you replace all that stuff up
to the values list with 'my @values = ', that would give you what you wanted
wouldn't it? The code below grabs the contents of the SQL string between (and
including) the first '(' and the last ')'. It then calls eval() to process it as
Perl source and assign the values to the array. Note that the escaping backslash
has been removed from the single quote. I hope this works for you.

Rob


use strict;
use warnings;

my $sql = q[INSERT INTO `rwikiobject` VALUES ('0b5e02f308c5341d0108fca900670107','2006-03-06 23:36:41','/site/ec07580d-1c66-469f-80be-c0afd616cedf/alembert, d \'','/site/ec07580d-1c66-469f-80be-c0afd616cedf');];

$sql =~ /(\(.*\))/;

my ($id, $version, $name, $realm) = eval $1;

print "$_\n" foreach ($id, $version, $name, $realm);


OUTPUT

0b5e02f308c5341d0108fca900670107
2006-03-06 23:36:41
/site/ec07580d-1c66-469f-80be-c0afd616cedf/alembert, d '
/site/ec07580d-1c66-469f-80be-c0afd616cedf


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to