WalaWiki content from p1k3.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.5 KiB

  1. WareLogging, PerlCode, RegularExpressions.
  2. * http://www.pcre.org/
  3. * perldoc: [http://perldoc.perl.org/perlreref.html perlreref] - RegEx reference
  4. * MasteringRegularExpressions
  5. Little RegEx tricks:
  6. # Stuff in parentheses is slurped into the elements of @results
  7. @results = $string =~ m/stuff, (more stuff), and (some other stuff)/;
  8. # Here, $result is forced into list context and so contains (more stuff).
  9. # Without parentheses, it would just contain 1 or 0, depending on the pattern match.
  10. ($result) = $string =~ m/stuff, (more stuff), and some other stuff/;
  11. Store a regex:
  12. $re = qr/whatever/;
  13. = a thing which has recently bitten me =
  14. When using s///x, keep in mind that whitespace continues to be significant in your substitution, which is after all a string.
  15. For example, this will be fine:
  16. $block =~ s/([^"\n]) # not a double-quote or newline
  17. \n # end-of-line
  18. ([^\n]) # not a newline
  19. /$1$newlines{$key}$2/xgs;
  20. While this will be all kinds of problematic:
  21. $block =~ s/([^"\n]) # not a double-quote or newline
  22. \n # end-of-line
  23. ([^\n]) # not a newline
  24. /$1
  25. $newlines{$key}
  26. $2
  27. /xgs;
  28. Incidentally, I think this is better written as:
  29. $block =~ s/(?<=[^"\n]) # not a double-quote or newline
  30. \n # end-of-line
  31. (?=[^\n]) # not a newline
  32. /$newlines{$key}/xgs;