Perl Python PHP RegEx CheatSheet
Perl Python PHP RegEx CheatSheet
About 1.1 Scalars and Strings (cont) 1.2 Arrays and Lists (cont)
This is version 2 of the perl reference card. e(E) scientific notation reverse(@a); reverse @a
(cl) 2008 Michael Goerz <goerz@physik.fu- f decimal floating point @a = sort{$ela <=> sort numerically
berlin.de>.
$elb}(@a);
g, G shorter %e or %f /
http://www.physik.fu-berlin.de/~goerz/
%E or %f @a = split(/-/,$s); split string into @a
Information taken liberally from the perl
documentation and various other sources. o signed octal $s = join(“, ” @c); join @a elements
You may freely distribute this document. into string
s string of chars
@a2 = @a[1,2,6..9]; array slice
u, x, X unsigned decimal int /
1 Variable Types
hex int / hex int in @a2 = grep(!/^#/, @a); remove comments
caps from @a
1.1 Scalars and Strings
p address pointer
Perl image
chomp($str); discard trailing \n n nothing printed
$v = chop($str); $v becomes trailing modifiers: h,l,L arg is short int / long
char int, double/ long
cmp More:
$str = “0” x 4; $str is now “0000” chr, crypt, hex, lc, q/STRING/,
$v = index($str, $x); find index of $x in $str, lcfirst, length, oct, ord, qq/STRING/, reverse,
pack uc, ucfirst
$v = rindex($str, $x); starting from left or right
3 References and Data Structures 3 References and Data Structures (cont) 2 Basic Syntax (cont)
$aref = \@a; reference to sub createcnt{ my $c=shift; closure, $c eval {$a=$a/$b; }; warn $@ exception
array return sub { print "$c++"; }; } persists if $@; handling
hash http://www.cheatography.com/mishin/cheat- c
$href ={APR => 4,AUG => anonymous hash sheets/perlcheat/ qr/pattern/imsox store regex in variable
last, next, redo end loop, skip to next, \z absolute string end
jump to top
\G continue from previous \w, \W word char [a-zA-Z0-9_] / non-word (?<=..), (? positive / negative look-behind
m//g char <!..)
[...] character set \s, \S whitepace [ \t\n\r\f] / non-space (?>...) prohibit backtracking
(...) group, capture to $1, $2 \C match a byte (?{ code embedded code
(?:...) group without capturing \pP, \PP match p-named unicode / non- })
(?#text) comment
if (@_) { $self->{NAME} = shift } -p same as -n, but will also print the -l enables automatic line-ending
return $self->{NAME}; contents of $_ processing in the output. Print
} statements will have the new line
Interactive perl -de1;use Term::ReadKey;
sub DESTROY { #destructor separator (\n) added at the end of
Mode:
my $self = shift; -- ${$self->{"_CENSUS"} };} each line.
http://szabgab.com/using-the-
1; # so the ‘require’ or ‘use’ succeeds
built-in-debugger-of-perl-as- -w prints any warning messages.
Using the class:
repl.html -e indicates that the following string
use Person;
is to be interpreted as a perl
$him = Person->new(); perl- http://www.thegeekstuff.com/2010/05
script (i.e., sequence of
$him->name("Jason"); debugger /perl-debugger/
commands).
printf "There's someone named %s.\n", $him- The Perl http://docstore.mik.ua/orelly/perl/pro
>name; Debugger g3/ch20_01.htm http://perldoc.perl.org/perlrun.html
use Data::Dumper; print Dumper($him); # Perl flags - perl -e '$x = "Hello world!n"; print
-T enables taint checking, which
debug pe, -pi, -p, $x;'
instructs perl to keep track of data
http://www.codeproject.com/Articles/3152/Perl- -w, -d, -i, -
from the user and avoid doing
Object-Oriented-Programming t? perldoc
anything insecure with it. Here this
http://ynonperek.com/course/perl/oo.html perlrun
option is used to avoid taking the
current directory name from the perl -MO=Deparse -p -e 1
Installing Modules:
@INC variable and listing the
perl -MO=Deparse -p -i -e 1
perl -MCPAN -e shell; available .pm files from the
perl -MO=Deparse -p -i.bak -e 1
directory recursively.
https://twitter.com/#!/perloneliner
8 One-Liners
- specify pattern for -a to use when splitting 2. just lines NOT between line 10 and 20
F perl -ne 'print unless 10 .. 20'
-i edit files in place
3. lines between START and END 14. If you had installed any modules from
CPAN, then you will need to re-install all of
perl -ne 'print if /START$/ .. / END$/'
them. (Naveed Massjouni)
4. in-place edit of *.c files changing all foo to
perl -E 'say for grep /site_perl/,@INC'| xargs
bar
find | perl -Fsite_perl/ -lane 'print $F[1] if
perl -pi.bak -e 's/\bfoo\b/bar/g' *.c /\.pm$/' | cpanm --reinstall
5. delete first 10 lines 15. Give executable rights to all perl file in dir
perl -i.old -ne 'print unless 1 .. 10' foo.txt find /home/client0/public_html -type f -name
'*.pl' -print0 | xargs -0 chmod 0755
6. change all the isolated oldvar occurrences to
newvar 16. Find files matching name-pattern
perl -i.old -pe 's{\boldvar\b}{newvar}g' *.[chy] https://gist.github.com/563679
Python sys Variables Python Class Special Methods Python String Methods (cont)
replace() utcoffset()
isoformat() dst()
__str__() tzname()
strftime(format)
%A Weekday (Sunday)
%p AM or PM
%w Weekday² (0 to 6)
%x Date
%X Time
%Y Year (2008)
array_push (arr, var1, var2 ...) filesize (file) [^abc] Not in range
U* Ungreedy pattern
a am or pm
* PCRE modifier
A AM or PM
g 12 Hour (1 to 12)
G 24 Hour (0 to 23)
w Day of week ¹ (0 to 6)
^ Start of string, or start of line in multi-line ?= Lookahead assertion . Any character except new line (\n)
pattern ?! Negative lookahead (a|b) a or b
\A Start of string
?<= Lookbehind assertion (...) Group
$ End of string, or end of line in multi-line
?!= or ?<! Negative lookbehind (?:...) Passive (non-capturing) group
pattern
?> Once-only Subexpression [abc] Range (a or b or c)
\Z End of string
?() Condition [if then] [^abc] Not (a or b or c)
\b Word boundary
?()| Condition [if then else] [a-q] Lower case letter from a to q
\B Not word boundary
?# Comment [A-Q] Upper case letter from A to Q
\< Start of word
[0-7] Digit from 0 to 7
\> End of word Quantifiers
\x Group/subpattern number "x"
* 0 or more {3} Exactly 3
Character Classes Ranges are inclusive.
+ 1 or more {3,} 3 or more
\c Control character
? 0 or 1 {3,5} 3, 4 or 5 Pattern Modifiers
\s White space
Add a ? to a quantifier to make it ungreedy. g Global match
\S Not white space
i* Case-insensitive
\d Digit Escape Sequences
m* Multiple lines
\D Not digit
\ Escape following character s* Treat string as single line
\w Word
\Q Begin literal sequence x* Allow comments and whitespace in
\W Not word
\E End literal sequence pattern
\x Hexadecimal digit
"Escaping" is a way of treating characters e* Evaluate replacement
\O Octal digit
which have a special meaning in regular U* Ungreedy pattern
expressions literally, rather than as special
POSIX * PCRE modifier
characters.