Regex question 
Author Message
 Regex question

Hi folks.

I'm trying to grab comments from the end of lines.
Shouldn't be too difficult, don't know why it's not
working.  I thought it worked last night....

Tried with and without the ? after groups 2/3;
no joy.


$log=1;

sub _preFormatNodes{

  local ($comment,$last_comment) = ("","");  
  print "Formatting ... " if $log;

   m/                   # Match ?  
    ^                   # from the begining of the scalar ?
    (.*)                # store in group 1 characters up to group 2:
    (%\s*)?             # store in group 2 the comment symbol and follwoing
whitespace
    (.*)?               # store in gruop 3 all following comment symbol ?
    ([\n\r\f])$         # up to a line break.
   /x;                  # Ignore whitespace in markup for legibility
   warn "Before:$_\n";
   $_ = $1;             # Replace entry with the matched characters minus
comment/brak
   warn "After :$_\n";
   if ($last_comment ne $3){    # If group 3 found a NEW comment ?
     $comment .= "$3\n";                # Store the new comment with a line
break.
     $last_comment = $3;                # Remember this comment
   }
  }
 print "...done.\n" if $log;
 # Return the array coerced to scalar
 print ">$comment\n";

Quote:
}


Node1: % comment
        <left1> == Nodepath:<path1>
        <left2> == "QuotedNodePath:<path2>"
        <left3> == "<quotedpath>"
        <left4> == atom
        <left5> == "quotedAtom"
        <left6> == <path>.

__END__;

Thanks in anticipation,
lee
--

Research Centre for Cognitive Science,
University of Sussex, Brighton UK



Sun, 09 Feb 2003 03:00:00 GMT  
 Regex question

Quote:

> $log=1;


It doesn't relate to your problem, but note that this is not the way
the function returns it's data (though it probably should).

Quote:
> sub _preFormatNodes{

>   local ($comment,$last_comment) = ("","");  
>   print "Formatting ... " if $log;

>    m/                   # Match ?  
>     ^                   # from the begining of the scalar ?
>     (.*)                # store in group 1 characters up to group 2:

Change this to:
      ([^%]*)

Quote:
>     (%\s*)?             # store in group 2 the comment symbol and follwoing
> whitespace
>     (.*)?               # store in gruop 3 all following comment symbol ?
>     ([\n\r\f])$         # up to a line break.
>    /x;                  # Ignore whitespace in markup for legibility

[snipped rest of code]

The way you have the RE, the first part can match everything and the
second and third parts are optional, so that's what happens -- the
first part matches everything.

But if you remove the "?" from, say, the second part, then the RE
doesn't match the other lines.  Of course, that isn't necessarily a
problem.  It is in your current code because you don't check to see if
the match is successful.  But if you stuck a 'next unless" in from of
the match, then it would be fine.

One more thing, when you replace $_ with $1, you probably want to
either append a newline, or append $4.  ( $_ = "$1$4"; )

--
Ren Maddox



Sun, 09 Feb 2003 03:00:00 GMT  
 Regex question

Quote:

> I'm trying to grab comments from the end of lines.
> Shouldn't be too difficult, don't know why it's not
> working.  I thought it worked last night....

You are forgetting two things:

  1) .* is greedy so in /(.*)(%\s+)?/ the second group _never_ matches
     (ignoring for now the fact that . doesn't match \n).

  2) If a m// does not match then $1, $2 etc are left unchanged.

--
     \\   ( )
  .  _\\__[oo

 .  l___\\
  # ll  l\\
 ###LL  LL\\



Sun, 09 Feb 2003 03:00:00 GMT  
 Regex question
Or you could just be fancy and use the pre and post match variables.  So
basicly:

/%\s*/;
$_ = $`;
$last_comment = $';

Not very readable I guess, but pretty slick.
--
-- Jared B. Luther
ICQ# 434884

http://www.xmission.com/~lex



Wed, 26 Feb 2003 04:29:06 GMT  
 Regex question

Jared> Or you could just be fancy and use the pre and post match variables.  So
Jared> basicly:

Jared> /%\s*/;
Jared> $_ = $`;
Jared> $last_comment = $';

Oops.  Hit one of my favorite "hot buttons".  You are looking at a
match variable without checking to see if the regex actually matched
or not.  If the match failed, you're looking at the *previous* value
of $`, and that'd ruin your day really fast because it's so
rediculously hard to debug.

So, change that to:

    if (/%\s*/) {
      $_ = $`;
      $last_comment = $';
    }

and I'll be much happier.  As should you. :)

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



Wed, 26 Feb 2003 10:45:02 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. how to program a simple text editor?

2. BUG in Delphi 2.0? (Another)

3. Setting Range or Filter in a master/detail

4. Need advanced HTML component

5. Reportsmith and Barcodes

6. convert c++ code to pascal

7. HOW TO GET DISK ID number

8. big interger unit

9. BP7 DPMI memory allocation

10. compiling TPW1.5 to 32 bits

11. desperate/runtime errors !?!?!?

12. TP7 -> FPK

 

 
Powered by phpBB® Forum Software