Perl Regex Help
I am looking to match strings out of a file that are prefixed
/**
and have a postfix
*/
With any number of characters/whitespace/newlines in between.
eg:
/** anything
anything
*/
I have m/(\/\*\*).*?(\*\/)/
so far, however this does not handle newlines. I know the fix has to be simple, but i have very limited regular expressions experience.
Add the s
modifier after it:
m/(\/\*\*).*?(\*\/)/s
But if it's source code you're operating on, be careful:
print 'a string /**';
int a = b + c;
print '*/';
// /**
a = a - c;
// */
There really is but one online resource if it comes to learning regex: http://www.regular-expressions.info/
Without using /s with make '.' behave differently, the following should work too:
m/(\/\*\*)(\r?\n|.)*(\*\/)/
For a place to learn perl?
http://perldoc.perl.org/ http://perldoc.perl.org/index-tutorials.html This is my ultimate reference, always.
But if you don't like to read something in a manual style which is a bit bore. Try Jeffrey Friedl's Mastering Regular Expressions from O'Reilly, which is more interesting.
http://oreilly.com/catalog/9781565922570
Your specific regex (with newlines) can be matched with \/\*\*[\d\D]*?\*\/
A side effect of \D
is that it matches newlines and can be used in this manner.
In Perl, you can also use Regexp::Common for finding a whole variety of source code comments.
There have already been mentioned some of the best links (Friedl's book and http://www.regular-expressions.info/)
My web sites for regex are these:
- Perl perlre tutorial. The best intro to Perl's regex.
- Perl perlre Perl's regex documentation
- Perl perlre quick reference The quick start guide
- Regular Expressions, A Favorite Parsetime
- Pattern Matching, Regular Expressions and Parsing Tutorials at Perlmonks
- Explain Regex
- RegExr Online Regular Expression Tester
- Regular Expression Library
- Regex Powertoy online tester
- BRE and ERE reference
- Larry Wall's Apocalypse 5: Pattern Matching
精彩评论