nested quantifier or unrecognized escape error

  • Thread starter Francois Massion
  • Start date
F

Francois Massion

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
 
M

Mirco Wahab

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
 
B

Brian McCauley

Francois said:
$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.
 
M

Mirco Wahab

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
 
F

Francois Massion

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
 
N

Nick of course

Mirco said:
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";
 
M

Mirco Wahab

Thus spoke Nick of course (on 2006-06-02 11:43):
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top