develooper Front page | perl.perlfaq.workers | Postings from January 2005

content suggestion

Thread Next
From:
Paul Apostolik
Date:
January 6, 2005 13:49
Subject:
content suggestion
Message ID:
41DDA1F9.5080907@softhome.net
While looking for information relating to regular expressions I 
came across the following:

   The perlfaq6 manpage: Regular Expressions
   http://faq.perl.org/perlfaq6.html#How_do_I_use_a_regul

The regular expression given to match C style multi-line 
comments is 72 characters long and very poorly reasoned out. I 
would like to present you with a much more concise alternative:

   /\*([^\*]*(\*[^/])*)*\*/

  The first step in working this problem will be to say exactly 
what a comment is in english:

   A comment starts with '/*', ends with '*/' and contains
   no '*/' in the middle.

   Matching the begining and ending seems simple enough - '/*' 
and '*/' accomplish this task. It is the middle that poses some 
difficulty. The regular expression needs to esure that if it 
ever consumes a '*' then the following character is not a '/'.

   So we can pass anything that is not a '*' without concern - 
'[^\*]*'. If we do see a '*' then the next character must be 
anything but a '/' - '(\*[^/])*'.

   Putting that all together gives us the regular expression we 
wanted:

   /\*([^\*]*(\*[^/])*)*\*/
      |---------------|*    1)
       |---|*               2)
             |------|*      3)

   1) allows 0 or more instances of either 2) or 3)
   2) allows non-star chars to pass
   3) requires any star char to be followed by a non-slash char

   You may use any part of this message in the perlfaq provided 
you do not publish my email address.

-- 

Paul Apostolik
     parabolis@softhome.net


The generation of random numbers is too important to be left to 
chance.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About