fusionplant.com


UTC 21:33:33
Thursday
1/18/2018



January 2018
SuMoTuWeThFrSa
 123456
78910111213
14151617181920
21222324252627
28293031   
       
Calendar Tool

welcome

Regular Expressions



Typical search and replace:
$pattern = "Hello, World!";
$pattern =~ s/World/Bob/;

$pattern is now set to "Hello, Bob!"

Some switches:
$pattern =~ s/h/4/ig;
i = ignore case, g = global (all occurances)


switch /x allows extended regex entries.
You can spread a pattern on many lines.
for example m/(hello) (world)/ can be:
m/
hello
\s
world
/x;
Be sure to switch off the use strict while doing this.
Use no strict; to switch it off and use strict; to switch it back on.


The /i switch does case insensitive matches.

The /s switch allows . (dot) to match newline characters (\n).




In a condition statement:
if ($pattern =~ m/bob/) { # where !~ negates
    ....code....
}




Search Patterns
* Matchs a pattern zero or more times
? Matches pattern once or none at all.
+ quantifier to match a pattern one or more times.
. A single dot, '.', matches any character except newline, '\n'
\t Tab character
\w Word character, or [a-zA-Z0-9_]. \W negates
\d Digits. Shortcut for [0-9]. \D negates
\b word boundry. Matches whole words. e.g., /\bpattern\b/ will match single word "pattern" but not "patternA" or "APattern"
[0-9] Finds 0 1 2 3 4 5 6 7 8 9
[a-z] Finds a b c d ... x y z. Also, [A-Z] finds A B C ... X Y Z
[abc$%!] Matches single characters: a, b, c, $, %, and !
[^abc] The ^ negates. Perl will not match these characters. e.g., [^\d] will not match digits.
\s Matches whitespace. Same as [\f\t\n\r ], form-feed, tab, newline, carriage return, and space character. \S negates.
() Parenthese are used to group patterns. Useful for quantifiers. e.g., /(pattern)+/. Used with memory parenthese are backreferences, \1, \2... So, /(["'])hello\1/ will match either quotes, hello, and which ever quote first found, that is, it will match "hello" and 'hello'. See also $1 which works after the search pattern.
{} General Quantifier matches pattern between n min and n max times. e.g., /x{3,9}/ this will match x anywhere from 3 times to 9 times. /x{5}/ matches x five times only.
| The pipe character is logic "or". e.g., /(thisOne)|(thatOne)/, will math "thisOne" or it will match "thatOne".
$`$&$' $& is the matched pattern. $` is what's before the match and $' is what's after the match
^$ ^ means string must start with this pattern. $ means string must end with this pattern. Kind of like left and right functions in some programming languages.


Replaceing:
$pattern =~ s/(World)/The $1 of Bob/;
$1 is whatever's inside of the first parentheses.


Search Patterns
$1 what's found in first parentheses. $2 is the second, etc...