nested quantifier or unrecognized escape error

Discussion in 'Perl Misc' started by Francois Massion, Jun 2, 2006.

  1. My problem:

    #!/usr/bin/perl

    $shortStr = 'CAD++ mit';
    $longStr = 'CAD++ mit zusätzlichen Scanning- und
    Digitalisierfunktionen';


    if($longStr =~ /$shortStr/){
    # Action...
    }

    #__end__

    (1) First I got a "nested quantifier" error message because of "++" in
    the regex.
    (2) After reading some contributions and the perlfunc entry on
    quotemeta I wrote:

    if($longStr =~ /\Q$shortStr\E/){

    and now I get a "unrecognized escape \q passed through at ..." message.

    Has anyone a suggestion?

    Thanks

    Francois
     
    Francois Massion, Jun 2, 2006
    #1
    1. Advertising

  2. Francois Massion

    Mirco Wahab Guest

    Thus spoke Francois Massion (on 2006-06-02 07:16):

    > $shortStr = 'CAD++ mit';
    > $longStr = 'CAD++ mit zusätzlichen Scanning- und Digitalisierfunktionen';
    > if($longStr =~ /$shortStr/){
    > # Action...
    > }
    > (1) First I got a "nested quantifier" error message because of "++" in
    > the regex.
    > (2) After reading some contributions and the perlfunc entry on
    > quotemeta I wrote:
    > if($longStr =~ /\Q$shortStr\E/){
    > and now I get a "unrecognized escape \q passed through at ..." message.


    you need iirc to 'quotemeta' the fragile string, like:

    ...
    my $shortStr = 'CAD++ mit';
    my $longStr = 'CAD++ mit zusätzlichen Scanning- und Digitalisierfunktionen';

    my $pat = quotemeta( $shortStr );
    if($longStr =~ /$pat/){
    print "match {". $pat ."}\n"; }
    ...

    prints:
    match {CAD\+\+\ mit}


    Regards

    Mirco
     
    Mirco Wahab, Jun 2, 2006
    #2
    1. Advertising

  3. Francois Massion wrote:

    > $longStr = 'CAD++ mit zusätzlichen Scanning- und
    > Digitalisierfunktionen';
    >
    >
    > if($longStr =~ /$shortStr/){
    > # Action...
    > }


    > (1) First I got a "nested quantifier" error message because of "++" in
    > the regex.
    > (2) After reading some contributions and the perlfunc entry on
    > quotemeta I wrote:
    >
    > if($longStr =~ /\Q$shortStr\E/){
    >
    > and now I get a "unrecognized escape \q passed through at ..." message.
    >
    > Has anyone a suggestion?


    I am unable to reproduce the problem using the code you posted.

    There is no \q in the code you posted and warnings are not enabled
    anyhow.

    Perhaps you corrected the typo when you retyped the code into the
    newsgroup.

    Please construct a _minimal_ test case and post it verbatim (cut and
    paste, do not retype).

    It's probably also worth saying what version of Perl you are using.
     
    Brian McCauley, Jun 2, 2006
    #3
  4. Francois Massion

    Mirco Wahab Guest

    Thus spoke Mirco Wahab (on 2006-06-02 09:43):

    > Thus spoke Francois Massion (on 2006-06-02 07:16):
    > ...
    > my $shortStr = 'CAD++ mit';
    > my $longStr = 'CAD++ mit zusätzlichen Scanning- und Digitalisierfunktionen';
    >
    > my $pat = quotemeta( $shortStr );
    > if($longStr =~ /$pat/){
    > print "match {". $pat ."}\n"; }
    > ...
    >
    > prints:
    > match {CAD\+\+\ mit}


    This:

    if($longStr =~ "\Q$shortStr\E") {
    print "match {". $pat ."}\n";
    }

    will work too, because
    "\Q...\E"
    resolves internally to the
    quotemeta() call mentioned
    above.

    Regards

    Mirco
     
    Mirco Wahab, Jun 2, 2006
    #4
  5. Hi Brian,

    I got it now. What happens is that I am using a text editor called
    Ultraedit. This editor can highlight the perl syntax. However it
    modified \Q to \q. After I disabled the highlighting of the syntax the
    automatic modification didn't occur and the script works.

    Thanks for the help and sorry for the trouble caused.

    Francois
     
    Francois Massion, Jun 2, 2006
    #5
  6. Mirco Wahab wrote:
    > if($longStr =~ "\Q$shortStr\E") {
    > print "match {". $pat ."}\n";
    > }


    I know this has nothing to do with the original problem but isn't it
    more readable as...

    print "match{$pat}\n";
     
    Nick of course, Jun 2, 2006
    #6
  7. Francois Massion

    Mirco Wahab Guest

    Thus spoke Nick of course (on 2006-06-02 11:43):

    > Mirco Wahab wrote:
    >> print "match {". $pat ."}\n";

    >
    > I know this has nothing to do with the original problem but isn't it
    > more readable as...
    >
    > print "match{$pat}\n";


    Of course it would ...

    But I'm shuddering on inserting
    block context delimiters _this way_
    into brackets when interpolation
    is wanted

    print "match {". $pat . "}\n";
    print "match {". do{$pat}. "}\n";

    So its just a matter of taste what
    means 'less ambiguity' for an
    'beginner==>intermediate' ;-)

    Regards

    Mirco
     
    Mirco Wahab, Jun 2, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -

    Greedy quantifier

    -, Jul 11, 2005, in forum: Java
    Replies:
    0
    Views:
    494
  2. Jeff

    Unrecognized escape sequence

    Jeff, Jun 30, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    39,098
    mansoorbhai
    Jun 21, 2011
  3. Douglas Alan
    Replies:
    66
    Views:
    2,375
    Nobody
    Aug 17, 2009
  4. Philippe Aymer
    Replies:
    12
    Views:
    228
    Philippe Aymer
    Oct 14, 2004
  5. Replies:
    19
    Views:
    441
    Dr.Ruud
    May 7, 2006
Loading...

Share This Page