开发者

Perl Regex Help

开发者 https://www.devze.com 2023-01-08 03:22 出处:网络
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.

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?


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:

  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
0

精彩评论

暂无评论...
验证码 换一张
取 消