zerocred has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a regex that i want to store as a multi-line var (with comments) at the top of the program - but when I make it multi-line it fails, when it is on a singel line it is OK. What am I doing wrong? This doesn't work - but if i take all the space and line feeds out it is OK.
my $regex =' ^(\w*)?\s (\w*)?\s (\w*)?\s (?:([A-Z]*)\s)? (\d+)\* (?:([0-9+]+)[>])? ([0-9+]+)\s. (\d*) ';
I use it like this - or at least this works if the regex is all one one line.
$input_string=~m/$regex/;

Replies are listed 'Best First'.
Re: how to write a multi-line regex
by holli (Abbot) on Jan 22, 2010 at 00:04 UTC
    You forgot the x-modifier. $input_string=~m/$regex/x;


    holli

    You can lead your users to water, but alas, you cannot drown them.
Re: how to write a multi-line regex
by Utilitarian (Vicar) on Jan 22, 2010 at 00:12 UTC
    You need to flag that you intend the regex to be multi line with comments using the x modifier. When quoting a regex use the qr operator:
    my $regex =qr{ ^(\w*)?\s #A comment (\w*)?\s # an other (\w*)?\s # and again (?:([A-Z]*)\s)? # all the way through (\d+)\* # so that the homicidal psychopath (?:([0-9+]+)[>])? # who knows where you live ([0-9+]+)\s. # and will have to maintain this code (\d*) # will think well of you }x; $input=~$regex
    http://http://www.perl.com/doc/manual/html/pod/perlop.html#item_qr for more detail

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: how to write a multi-line regex
by rkrieger (Friar) on Jan 22, 2010 at 00:19 UTC

    You may want to take a look at perlre and perlretut. I usually pick something such as below (admittedly picked it up from Perl Best Practices by Damian Conway). I like to use the 'xms' modifiers:

    my $line = qr{ \A # Start of string \s* # Leading blanks \w+ # Key \s* \w+ # Value \s* # Trailing blanks \z # End of string }xms;

    Alternatively, I use Regexp::Common as well if I want to build something up from components. It's not really what you were asking, but it may be handy.

      Thanks for the replies. I see it now - I didn't have the /x on the end - or I put it in the $regex (but incorrectly). So either put the /x on the end where it is used in the code or put the whole thing "m/abc/x" in $regex. Ta much.

        If you want to put the x inside the regex you can do it like this (ripping off Utilitarian's code):

        my $regex =qr{(?x) ^(\w*)?\s #A comment (\w*)?\s # an other (\w*)?\s # and again (?:([A-Z]*)\s)? # all the way through (\d+)\* # so that the homicidal psychopath (?:([0-9+]+)[>])? # who knows where you live ([0-9+]+)\s. # and will have to maintain this code (\d*) # will think well of you };

        Note that the (?x) must immediately follow the first regex delimiter otherwise the whitespace before will be a literal part of the pattern (this was not the case with earlier Perl versions, 5.6 IIRC) and note also that using qr{...} creates a compiled regex which you assign to a scalar for later use. The nice thing about the (?x) syntax is the ability to switch behaviours on and off for different regions in your pattern whereas the /.../x modifier applies to the whole pattern. This is probably more useful with something like case insensitivity and such use is illustrated in this reply in a thread started by Win.

        I hope this is helpful.

        Cheers,

        JohnGG