content suggestion
[email protected] (Paul Apostolik) Thu, 06 Jan 2005 15:39:21 -0500
| Newsgroups | perl.perlfaq.workers |
|---|---|
| Message-ID | <[email protected]> |
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
[email protected]
The generation of random numbers is too important to be left to
chance.