Regexp - I must be blind 
Author Message
 Regexp - I must be blind

: Could any kind soul open my eyes on what's going wrong with the following
:
:
:
: $text= '   if ( cond ) then; ';
: print "$1||$2\n" if $text =~ /(\sif\s*\([^;]*\)\s*)([^{;]+);/;
: # this prints(as expected)    if ( cond ) ||then
:
: $text =~ s/(\sif\s*\([^;]*\)\s*)([^{;]+);/$1{$2};/;
                                              ^
                                             ^
                                            ^
                             Problem is here

Perl is interpreting the bracket as a variable subscript --
it has to otherwise you couldn't use $hash{$key}.

 $text =~ s/(\sif\s*\([^;]*\)\s*)([^{;]+);/${1}{$2};/;

The above should work as you intend.

--
Regards,
Mike Heins     [mailed and posted]   http://www.*-*-*.com/ ~mikeh ___       ___
                                    Internet Robotics        |_ _|____ |_ _|
Few blame themselves until they     131 Willow Lane, Floor 2  | ||  _ \ | |
have exhausted all other            Oxford, OH  45056         | || |_) || |
possibilities.                                               |___|  _ <|___|



Mon, 04 Jan 1999 03:00:00 GMT  
 Regexp - I must be blind

Could any kind soul open my eyes on what's going wrong with the following

$text= '   if ( cond ) then; ';
print "$1||$2\n" if $text =~ /(\sif\s*\([^;]*\)\s*)([^{;]+);/;
# this prints(as expected)    if ( cond ) ||then

$text =~ s/(\sif\s*\([^;]*\)\s*)([^{;]+);/$1{$2};/;
# Use of uninitialized value at ... line 5
print ">>$text<<\n";
# this prints   >>  ; <<

and is this a bug of feature ?  Perl doesn't like  ( ) within a   (?#   ) construct

$text= 'abc';
$text =~ s/abc  (?# this is a ( comment ) with parenthesis )
          /     (?# here is another comment)
           def/x;
--> Error           /: unmatched () in regexp at ... line 4

Thanks for any hints,
Helmut Jarausch.



Mon, 04 Jan 1999 03:00:00 GMT  
 Regexp - I must be blind


Quote:

>Could any kind soul open my eyes on what's going wrong with the following

....
....
....
Quote:
>$text= '   if ( cond ) then; ';
....
>$text =~ s/(\sif\s*\([^;]*\)\s*)([^{;]+);/$1{$2};/;
># Use of uninitialized value at ... line 5

....
....
Well, I just couldn't resist this one.
From one (genuinely) blind person, to someone who thinks he must be:
As others before me have mentioned, the line 5 error is the '/$1{$2}/' which
is interpreted as a hash.  Escape the braces with '\', and it will work.

Quote:
>and is this a bug of feature ?  Perl doesn't like  ( ) within a
> (?#   ) construct

....
....
Quote:
>$text =~ s/abc  (?# this is a ( comment ) with parenthesis )

....
....
Quote:
>--> Error           /: unmatched () in regexp at ... line 4

I'm sure this is not a bug or feature, but if you want to put parenthesis
in a comment, you'd better put a '\' before the right one ')' or the comment
will be prematurely ended, (which, I believe is the source of your error.)
Quote:

>Thanks for any hints,
>Helmut Jarausch.

Hey, no problem!!!


Tue, 05 Jan 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. newbie-perl or why am i blind to this simple

2. Why am I getting this regexp error?

3. Blind Vigilantes

4. Blind Vigilantes

5. Perl for the Blind

6. Blind Vigilantes

7. Syntax blind-spot

8. Blind Vigilantes

9. Blind copy with sendmail

10. keyboard input in BLIND MODE

11. I must be blind...

12. Blind Vigilantes

 

 
Powered by phpBB® Forum Software