View Full Version : perl regexp to match bash comments
roccivic
December 17th, 2010, 07:10 PM
Hi folks,
I'm having a really hard time writing what should be a really straightforward PERL regular expression. I need to match all comments in PNM and PGM image files. So I need to match strings that:
can start with anything
have a hash symbol '#'
are followed by any character except for '\n', '\r' or '\r\n'
are terminated by '\n', '\r' or '\r\n'
So far I tried this (which of course does not work)
$data = preg_replace( '/#./m', '', $data );
Arndt
December 17th, 2010, 08:50 PM
Hi folks,
I'm having a really hard time writing what should be a really straightforward PERL regular expression. I need to match all comments in PNM and PGM image files. So I need to match strings that:
can start with anything
have a hash symbol '#'
are followed by any character except for '\n', '\r' or '\r\n'
are terminated by '\n', '\r' or '\r\n'
So far I tried this (which of course does not work)
$data = preg_replace( '/#./m', '', $data );
What about this?
$data = preg_replace( '/#.*/m', '', $data );
roccivic
December 17th, 2010, 09:12 PM
What about this?
$data = preg_replace( '/#.*/m', '', $data );
It works great, thanks.
Was only one character away...
shawnhcorey
December 18th, 2010, 01:51 AM
Hi folks,
I'm having a really hard time writing what should be a really straightforward PERL regular expression. I need to match all comments in PNM and PGM image files. So I need to match strings that:
can start with anything
have a hash symbol '#'
are followed by any character except for '\n', '\r' or '\r\n'
are terminated by '\n', '\r' or '\r\n'
So far I tried this (which of course does not work)
$data = preg_replace( '/#./m', '', $data );
Why are you asking for Perl and then write in PHP?
roccivic
December 18th, 2010, 03:09 AM
That's cause PHP uses Perl Compatible Regular Expressions: http://www.php.net/manual/en/ref.pcre.php (as well as the deprecated POSIX regex).
shawnhcorey
December 18th, 2010, 03:41 AM
That's cause PHP uses Perl Compatible Regular Expressions: http://www.php.net/manual/en/ref.pcre.php (as well as the deprecated POSIX regex).
OK, if you say so:
m{ \# [^\r\n] .*? (?: \r\n | \r | \n ) }msx;
roccivic
December 18th, 2010, 04:31 AM
Your regexp also works great, although I had to strip the spaces and curly brackets:
$data = preg_replace( '/#[^\r\n].*?(?:\r\n|\r|\n)/m', '', $data );
Thanks ;)
shawnhcorey
December 18th, 2010, 03:58 PM
Your regexp also works great, although I had to strip the spaces and curly brackets:
The curly brackets are delimiters. The /x option at the end allows whitespace in the pattern.
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.