Miscellaneous: Regual expressions

(last edit: 2000-11-22) /abc/ is regual expression for: grep abc somefile m#/usr/etc# using # for a delimiter equal to /\/usr\/etc/ open(IN, "<somefile") || [exit the script] while(<IN>) { if(/abc/) { print $_; } } [abcde] matches a or b or c or d or e [0-9] matches 0 or 1 or 2 ... or 9 [0-9\-] matches 0-9 or minus [a-z0-9] matches any single lowercase letter or digit [^0-9] matches any single non-digit [^\^] matches single characters except an up-arrow bla|bli matches or 'bla' or 'bli' \d a digit \D not a digit \w word \W not a word \s space \S not a space \b word boundary \B no word boundary /fred\b/ matches 'fred' but not 'frederick' /\bmo/ matches 'moe', 'mole' but not 'elmo' /\bFred\B/ matches 'Frederick' but not 'fred flinstone' . any single non-newline character * zero of more + one or more ? zero or one ^ beginning of string $ end of string ^a matches an 'a' if and only if the 'a' is the first charcter of the string a^ ^ looses its meaning and this expression matches 'a' and '^' $a see ^a a$ see a^ x{5,10} five to ten x's x{5,} five or more x's x{5} five x's x{0,5} five or less x's () grouping operator, causes the part of the string matchedc by the parrern to be remebered. /fred(.)barney\1/ matches a strin consisting of fred, followd by any single non-newline character followed by barney, followed by that smae singe character /fred(.)barney/ print $1 prints the single non-newline character found by the '.' operator /fred(.)barney(.)wilma\1\2/ print $1 print $2 s/abc/def/ substitutes 'abc' for 'def' s/abc/i ignores case s/abc/g substitutes all possible matches in string s#abc#def# same as s/abc/def/ use this if th '/' is in the string you like to alter Grouping precedence Parantheses ( ) (?: ) Multipliers ? + * {m,n} ?? +? *? {m,n}? Sequence and anchoring abc ^ $ \A \Z (?= ) (?! ) Alternation |

Click here to go back to the index.