Difference between revisions of "Perl regex"
Jump to navigation
Jump to search
m (post-import cleanup of navbar) |
(→Details: () replacement vars; formatter) |
||
Line 6: | Line 6: | ||
==Details== | ==Details== | ||
Special characters in regex: | Special characters in regex: | ||
− | * . = any character | + | * '''.''' = any character |
− | * * = 0 or more of previous character | + | * '''*''' = 0 or more of previous character |
− | * ^ = following string begins the line (except [^...] means "not these characters") | + | * '''^''' = following string begins the line (except [^...] means "not these characters") |
− | * $ = preceding string ends the line | + | * '''$''' = preceding string ends the line |
− | * [] = list of characters which can satisfy the match at this position | + | * '''[]''' = list of characters which can satisfy the match at this position |
− | * {} = # of repetitions of previous character | + | * '''{}''' = # of repetitions of previous character |
− | * | = alternatives | + | * '''()''' = part of input string to be plugged into variables ($1, $2, etc.) |
− | * + = 1 or more of previous character | + | * '''|''' = alternatives |
− | * ''a''-''b'' = range of characters from ''a'' to ''b'' (must be inside [] to be position-sensitive?) | + | * '''+''' = 1 or more of previous character |
+ | * ''a'' - ''b'' = range of characters from ''a'' to ''b'' (must be inside [] to be position-sensitive?) | ||
+ | |||
+ | Replacement variables generated by () are | ||
+ | * automatically numbered beginning with $1 | ||
+ | * not local to the regex statement (i.e. they are available in subsequent lines of code) | ||
Operators used to invoke regex: | Operators used to invoke regex: | ||
− | * =~ returns TRUE if pattern matches | + | * '''=~''' returns TRUE if pattern matches |
− | * !~ returns FALSE if pattern matches | + | * '''!~''' returns FALSE if pattern matches |
− | * s/''pattern''/''replacement''/ | + | * '''s/'''<u>pattern</u>'''/'''<u>replacement</u>'''/''' replaces <u>pattern</u> with <u>replacement</u> |
+ | |||
==Examples== | ==Examples== | ||
* Replace "thingy" with "stuffs" in $string: | * Replace "thingy" with "stuffs" in $string: | ||
** $string = s/thingy/stuffs/; | ** $string = s/thingy/stuffs/; |
Revision as of 18:43, 3 March 2006
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
- () = part of input string to be plugged into variables ($1, $2, etc.)
- | = alternatives
- + = 1 or more of previous character
- a - b = range of characters from a to b (must be inside [] to be position-sensitive?)
Replacement variables generated by () are
- automatically numbered beginning with $1
- not local to the regex statement (i.e. they are available in subsequent lines of code)
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/;