Wednesday, February 26, 2014

Handy PERL regular expressions



Pattern

Description
/a./ The . matches any singe character except newline(\n)  eg.ab a
/[abcde]/

Matches a sting containing any one of the first five letters a lowercase alphabet eg.dog box
/[aeiouAEIOU]/ Matches any of the five vowels in either lower or uppercase eg.India egg
/[0123456789]/ Match if any single digit is present  eg. 0 3 41 100
/[0-9]/    Matches any single digit like the above                0 3
/[0-9\-]/ Match 0-9, or minus                        - 0 3
/[a-z0-9]/ Match any single lowercase letter or digit            a 0 9
/[a-zA-Z0-9_]/ Match any single letter, digit, or underscore            a X 9 _
/[^0-9]/ Match any single non-digit                    a
/[^aeiouAEIOU] Match any single non-vowel                    M N L
/[^\^]/ Match single character except an up-arrow            M N L
/[\da-fA-F]/ Match one hex digit
/fo+ba?r/ Matches an f followed by one or more o's followed by a b, followed by an optional a, followed by an r.
s/x+/boom/ Always replaces all consecutive x's with boom (resulting in fred boom barney), rather than just one or two x's, even though a shorter set of x's would also match the same regular expression. ie. $_ = "fred xxxxxxxxxx barney";
/x{5,10}/ Matches five to ten x's
/x{5,}/ Means five or more in this case
/x{5}/  Means "exactly this many" (five x's).
/x{0,5}/ Matches five or less x's
/a.{5}b/ Matches the letter a separated from the letter b by any five non-newline characters at any point in the string.
/a.*?c.*d/ matches the fewest characters between the a and c, not the most characters.
/oracle/ True if oracle matches. Note this will also match XoracleY
/\boracle\b/ True if only oracle matches
/ora\b/    True for anything that ends with ora, but fails for oracle. The \b is the word boundary.
/\bo/ True for oracle oozie operator, basically anything that begins with o
/\bora/ True only for anything that begins with ora
/^ora/ True only if it begins with ora, orc will fail
/^(o|h)/ True for only words begining with o or h
/^x|y/ Matches x at the beginning of line, or y anywhere
/abc*/ Matches ab, abc, abcc, abccc, abcccc, cab, zabc
/a|bc|d/ Matches a, bc, d, but not xyz, xbz
/(a|b)(c|d)/ Matches ac, ad, bc, or bd
/(song|blue)bird/ Matches songbird or bluebird
if (<STDIN> =~ /^y/i) True if the input begin with a y
s/foo/bar/g Replaces foo with bar in the string $_