Skip to content

Latest commit

 

History

History
93 lines (75 loc) · 2.63 KB

File metadata and controls

93 lines (75 loc) · 2.63 KB

reference_perl_operators.md

Regular Expression Reference

Perl specific operators

m/regex/mods - simple match

echo "Russia" | perl -ne 'print "yes\n" if(/russ/i);'

s/regex/replacement/mods - substitute (or count)
substitute and (if needed) gives back how often the match has been substituted

echo "superman" | perl -pe 's/man/woman/;' # output: superwoman
echo "abc" | perl -pe 'print s/ab/X/;'     # output: 1Xc
echo "abcdabdeab" | perl -pe '$a= s/ab/X/g; print "---$a---\n";'
# output:
# ---3---
# XcdXdeX

tr/before/after/ - transliteration operator
the tr operator does not accept regexes, everything is treated as a character

echo "paperino" | perl -pe 'tr/pao/PAO/;' # output: PAPerinO
echo "p.*" | perl -pe 'tr/p.*/P?+/;'      # output: P?+

** =~ !~ - positive and negative match operators**


regex delimiter - how can I delimit my regex /.../
/ works, but you can use almost every non alphanumeric simple visible ascii char
the reason can be: save as many backslashes as possible

echo "a&b&c" | perl -pe 's/&/X/g;'             # output: aXbXc
echo "a/b/c" | perl -pe 's&/&X&g;'             # output: aXbXc
echo "a/b/c" | perl -pe 's+/+X+g;'             # output: aXbXc
echo "a*b+c/d&e" | perl -pe 's#[*+/&]#X#g;'    #output: aXbXcXdXe

built in variable
$` text before match
$& text matched
$' text after match
$1 text matched with first parentheses
$2 text matched with second parentheses
$+ text matched with highest numbered parentheses
$^N text matched with most recently closed parentheses
@- array of match-start indices into target text
@+ array of match-end indices into target text
for better efficiency avoid $& $' $` and backref where possible


pos() Used to obtain the actual position of the match. But you can also use the function to set the start of the match. NEVER FORGET the g modifier when doing it!

perl -e '$a="a1a2a3"; pos($a)=3; if($a =~ /(a.)/g){print "$1\n";}'    # output: a3

(X|Y) - about alternation
Some Implementation study the length of X and Y in order to support greedy alternation.
This may take a longer time.
Perl always takes the left first match of an alternation.

echo "ab" | perl -pe 's/(ab|a)/X/;'   # output: X
echo "ab" | perl -pe 's/(a|ab)/X/;'   # output: Xb

study()
When the string to analyze is very long and you need to match many regexex on it, you can first study it.

echo 'dudududadada' | perl -pe 'study($_); s/(u|a)/X/g;'     # output: dXdXdXdXdXdX