tags:

views:

80

answers:

3

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.
Bonus question: Does anyone have a good website for learning regular expressions?

+3  A: 

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/

Bart Kiers
Note that I'm not saying there don't exist other good online resources, but the one I posted should be sufficient for anyone new to regex to get a good understanding of how to use regex-es.
Bart Kiers
Works, also thanks for the website.
James
A: 

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

ttchong
Instead of `(\r?\n|.)`, you could do: `[\s\S]`.
Bart Kiers
A: 

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:

  1. Perl perlre tutorial. The best intro to Perl's regex.
  2. Perl perlre Perl's regex documentation
  3. Perl perlre quick reference The quick start guide
  4. Regular Expressions, A Favorite Parsetime
  5. Pattern Matching, Regular Expressions and Parsing Tutorials at Perlmonks
  6. Explain Regex
  7. RegExr Online Regular Expression Tester
  8. Regular Expression Library
  9. Regex Powertoy online tester
  10. BRE and ERE reference
  11. Larry Wall's Apocalypse 5: Pattern Matching
drewk