Difference between revisions of "Perl regex"

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
Jump to navigation Jump to search
 
m (post-import cleanup of navbar)
Line 1: Line 1:
[[Category:Techniques]]
+
[[Computing]]: [[Programming]]: [[Perl]]: [[Perl regex|regex]]
[[Techniques]]: [[Perl]]: [[Perl regex|regex]]
 
  
 
This article explains regular expressions in terms understandable to mere mortals, and also how to use them in Perl.
 
This article explains regular expressions in terms understandable to mere mortals, and also how to use them in Perl.

Revision as of 21:28, 14 October 2005

Computing: Programming: Perl: regex

This article explains regular expressions in terms understandable to mere mortals, and also how to use them in Perl.

Related Articles

  • regex: manpage documentation

Details

Special characters in regex:

  • . = any character
  • * = 0 or more of previous character
  • ^ = following string begins the line (except [^...] means "not these characters")
  • $ = preceding string ends the line
  • [] = list of characters which can satisfy the match at this position
  • {} = # of repetitions of previous character
  • | = alternatives
  • + = 1 or more of previous character
  • a-b = range of characters from a to b (must be inside [] to be position-sensitive?)

Operators used to invoke regex:

  • =~ returns TRUE if pattern matches
  • !~ returns FALSE if pattern matches
  • s/pattern/replacement/ replaces pattern with replacement

Examples

  • Replace "thingy" with "stuffs" in $string:
    • $string = s/thingy/stuffs/;