Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: regex is not working as I intended

by hippo (Bishop)
on Jan 18, 2018 at 23:23 UTC ( [id://1207490]=note: print w/replies, xml ) Need Help??


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:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1207490]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-25 19:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found