Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Regex Search pattern not terminated error

by neilwatson (Priest)
on Mar 12, 2016 at 15:04 UTC ( #1157546=perlquestion: print w/replies, xml ) Need Help??
neilwatson has asked for the wisdom of the Perl Monks concerning the following question:

Greetings. This code returns the error 'Search pattern not terminated'. I think it's the '{' inside the regex, but shouldn't that be automatically escaped by \Q\E?

#!/usr/bin/env perl use strict; use warnings; my $regex = qr{ \n (?: \QI'll be good.\E | Hushing | Hrumph | \Q>:[\E | \Q:-(\E | \Q:(\E | \Q:-c\E | \Q:c\E | \Q:-<\E | \Q:< +\E | \Q:-[\E | \Q:[\E | \Q:{\E | \Q:-|\E | \Q:@\E | \Q>:(\E | \Q:' +-(\E # here? ^^^^^ | \Q:'(\E | \QShutting up now.\E | \QBut, but...\E | \QI'll be quiet.\E ) }mix;

Neil Watson

Replies are listed 'Best First'.
Re: Regex Search pattern not terminated error
by Athanasius (Bishop) on Mar 12, 2016 at 15:32 UTC

    Hello neilwatson,

    You are right about two things:

    1. the problem expression is \Q:{\E
    2. \Q ... \E (and quotemeta) do indeed escape braces (curly brackets) along with the other regular expression metacharacters.

    What’s happening here (and it took me a while to figure it out!) is that the qr expression is also using braces as its delimiter. So the Perl parser first looks for the end of the whole qr{...} expression, and thinks it has found it when it encounters the first right brace; only later does it implement \Q ... \E to backslash non-word characters.

    To fix this, either escape the brace manually (\Q:\{\E) or (better) change the delimiter: my $regex = qr! \n ... !mix;

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Regex Search pattern not terminated error
by ww (Archbishop) on Mar 12, 2016 at 15:51 UTC

    It appears that Athanasius1 hit the nail on the head... first shot.

    Since my foo didn't provide that insight directly, I tried adding use diagnostics; which produced this illuminating comment:

    Search pattern not terminated at D:\_Perl_\PMonks\ line 7 (# +1) (F) The lexer couldn't find the final delimiter of a // or m{} construct. Remember that bracketing delimiters count nesting leve +l. Missing the leading $ from a variable $m may cause this error. Note that since Perl 5.9.0 a // can also be the defined-or construct, not just the empty search pattern. Therefore code writ +ten in Perl 5.9.0 or later that uses the // as the defined-or can be misparsed by pre-5.9.0 Perls as a non-terminated search pattern. Uncaught exception from user code: Search pattern not terminated at D:\_Perl_\PMonks\ l +ine 7.

    The confirmation there of your surmise should lead pretty directly to (somewhat obscure) documentation (that I didn't bother to chase down because you already had a valid answer from the Reverend Canon.

    JBTW, my error line as cited by diagnostics is off-by-one because I add a node number to the test code.

    Update 1 ...and choroba who offered similar wisdom as I was preparing this.

Re: Regex Search pattern not terminated error
by choroba (Bishop) on Mar 12, 2016 at 15:41 UTC
    The problem is that you need to get
    my $regex2 = qr% \n (?: \QI'll be good.\E | Hushing | Hrumph | \Q>:[\E | \Q:-(\E | \Q:(\E | \Q:-c\E | \Q:c\E | \Q:-<\E | \Q:< +\E | \Q:-[\E | \Q:[\E | :\{ | \Q:-|\E | \Q:@\E | \Q>:(\E | \Q:'-(\ +E | \Q:'(\E | \QShutting up now.\E | \QBut, but...\E | \QI'll be quiet.\E ) %mix;

    But, as curly braces are the delimiters, qr{ \{ } turns into qr% { %', , but there's no way how to express 'qr% \{ % with curlies as the delimiters - but you don't need that, as they're equivalent.

    That's why I don't like using curlies as regex delimiters.

    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my @res = ( qr% a{1 %, qr{ a\{1 }, qr% a\{1 %, qr% a\Q{\E1 %, qr{ a\Q\{\E1 }, qr% a{{1}1 %, qr{ a\{{1}1 }, qr{ a\{{1}1 }, qr{ a\{\{1\}1 }, qr% a\{{1}1 %, ); say $_, ' a{1 ' =~ $_ for @res;

    Update: more examples in the second code.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1157546]
Front-paged by Athanasius
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2019-04-23 04:50 GMT
Find Nodes?
    Voting Booth?
    I am most likely to install a new module from CPAN if:

    Results (114 votes). Check out past polls.