in reply to regex is not working as I intended

Why are the first two alternatives not capturing quoted test strings?

Because your position is still at the string start so you can't use a lookbehind (or if you do it will never match). Simpler just to use this:

use strict; use warnings; use Test::More tests => 3; my @strings = ( 'Now is the time', '"Now is the time"', "'Now is the time'", ); my $regex = qr/^['"]?(.*?)['"]?$/; for my $input (@strings) { my ($match) = ($input =~ /$regex/); is $match, 'Now is the time', "$input matched"; }

Replies are listed 'Best First'.
Re^2: regex is not working as I intended
by AnomalousMonk (Archbishop) on Jan 19, 2018 at 03:33 UTC
    my $regex = qr/^['"]?(.*?)['"]?$/;

    Note that this regex will also match oddballs like  q{'Now is the time} and  q{'Now is the time"}


    Give a man a fish:  <%-{-{-{-<