P e r l d o c B r o w s e r
●
5 . 1 6 . 2
L a t e s t
5 . 4 0 . 0
5 . 3 8 . 2
5 . 3 8 . 1
5 . 3 8 . 0
5 . 3 6 . 3
5 . 3 6 . 2
5 . 3 6 . 1
5 . 3 6 . 0
5 . 3 4 . 3
5 . 3 4 . 2
5 . 3 4 . 1
5 . 3 4 . 0
5 . 3 2 . 1
5 . 3 2 . 0
5 . 3 0 . 3
5 . 3 0 . 2
5 . 3 0 . 1
5 . 3 0 . 0
5 . 2 8 . 3
5 . 2 8 . 2
5 . 2 8 . 1
5 . 2 8 . 0
5 . 2 6 . 3
5 . 2 6 . 2
5 . 2 6 . 1
5 . 2 6 . 0
5 . 2 4 . 4
5 . 2 4 . 3
5 . 2 4 . 2
5 . 2 4 . 1
5 . 2 4 . 0
5 . 2 2 . 4
5 . 2 2 . 3
5 . 2 2 . 2
5 . 2 2 . 1
5 . 2 2 . 0
5 . 2 0 . 3
5 . 2 0 . 2
5 . 2 0 . 1
5 . 2 0 . 0
5 . 1 8 . 4
5 . 1 8 . 3
5 . 1 8 . 2
5 . 1 8 . 1
5 . 1 8 . 0
5 . 1 6 . 3
5 . 1 6 . 2
5 . 1 6 . 1
5 . 1 6 . 0
5 . 1 4 . 4
5 . 1 4 . 3
5 . 1 4 . 2
5 . 1 4 . 1
5 . 1 4 . 0
5 . 1 2 . 5
5 . 1 2 . 4
5 . 1 2 . 3
5 . 1 2 . 2
5 . 1 2 . 1
5 . 1 2 . 0
5 . 1 0 . 1
5 . 1 0 . 0
5 . 8 . 9
5 . 8 . 8
5 . 8 . 7
5 . 8 . 6
5 . 8 . 5
5 . 8 . 4
5 . 8 . 3
5 . 8 . 2
5 . 8 . 1
5 . 8 . 0
5 . 6 . 2
5 . 6 . 1
5 . 6 . 0
5 . 0 0 5 _ 0 4
5 . 0 0 5 _ 0 3
5 . 0 0 5 _ 0 2
5 . 0 0 5 _ 0 1
5 . 0 0 5
●
D e v
b l e a d
5 . 4 1 . 2
5 . 4 1 . 1
5 . 4 0 . 0 - R C 2
5 . 4 0 . 0 - R C 1
5 . 3 9 . 1 0
5 . 3 9 . 9
5 . 3 9 . 8
5 . 3 9 . 6
5 . 3 9 . 5
5 . 3 9 . 4
5 . 3 9 . 3
5 . 3 9 . 2
5 . 3 9 . 1
5 . 3 7 . 1 1
5 . 3 7 . 1 0
5 . 3 7 . 9
5 . 3 7 . 8
5 . 3 7 . 7
5 . 3 7 . 6
5 . 3 7 . 5
5 . 3 7 . 4
5 . 3 7 . 3
5 . 3 7 . 2
5 . 3 7 . 1
5 . 3 7 . 0
●
D o c u m e n t a t i o n
P e r l
I n t r o
T u t o r i a l s
F A Q s
R e f e r e n c e
O p e r a t o r s
F u n c t i o n s
V a r i a b l e s
M o d u l e s
U t i l i t i e s
C o m m u n i t y
H i s t o r y
E x p a n d
p e r l r e
( s o u r c e ,
C P A N )
Y o u a r e v i e w i n g t h e v e r s i o n o f t h i s d o c u m e n t a t i o n f r o m P e r l 5 . 1 6 . 2 .
V i e w t h e l a t e s t v e r s i o n
●
N A M E
●
D E S C R I P T I O N
●
M o d i f i e r s
●
/ x
●
C h a r a c t e r s e t m o d i f i e r s
●
/ l
●
/ u
●
/ d
●
/ a ( a n d / a a )
●
W h i c h c h a r a c t e r s e t m o d i f i e r i s i n e f f e c t ?
●
C h a r a c t e r s e t m o d i f i e r b e h a v i o r p r i o r t o P e r l 5 . 1 4
●
R e g u l a r E x p r e s s i o n s
●
M e t a c h a r a c t e r s
●
Q u a n t i f i e r s
●
E s c a p e s e q u e n c e s
●
C h a r a c t e r C l a s s e s a n d o t h e r S p e c i a l E s c a p e s
●
A s s e r t i o n s
●
C a p t u r e g r o u p s
●
Q u o t i n g m e t a c h a r a c t e r s
●
E x t e n d e d P a t t e r n s
●
S p e c i a l B a c k t r a c k i n g C o n t r o l V e r b s
●
B a c k t r a c k i n g
●
V e r s i o n 8 R e g u l a r E x p r e s s i o n s
●
W a r n i n g o n \ 1 I n s t e a d o f $ 1
●
R e p e a t e d P a t t e r n s M a t c h i n g a Z e r o - l e n g t h S u b s t r i n g
●
C o m b i n i n g R E P i e c e s
●
C r e a t i n g C u s t o m R E E n g i n e s
●
P C R E / P y t h o n S u p p o r t
●
B U G S
●
S E E A L S O
# N A M E
p e r l r e - P e r l r e g u l a r e x p r e s s i o n s
# D E S C R I P T I O N
T h i s p a g e d e s c r i b e s t h e s y n t a x o f r e g u l a r e x p r e s s i o n s i n P e r l .
I f y o u h a v e n ' t u s e d r e g u l a r e x p r e s s i o n s b e f o r e , a q u i c k - s t a r t i n t r o d u c t i o n i s a v a i l a b l e i n p e r l r e q u i c k , a n d a l o n g e r t u t o r i a l i n t r o d u c t i o n i s a v a i l a b l e i n p e r l r e t u t .
F o r r e f e r e n c e o n h o w r e g u l a r e x p r e s s i o n s a r e u s e d i n m a t c h i n g o p e r a t i o n s , p l u s v a r i o u s e x a m p l e s o f t h e s a m e , s e e d i s c u s s i o n s o f m / /
, s / / /
, q r / /
a n d ??
in " R e g e x p Q u o t e - L i k e O p e r a t o r s " i n p e r l o p .
# M o d i f i e r s
M a t c h i n g o p e r a t i o n s c a n h a v e v a r i o u s m o d i f i e r s . M o d i f i e r s t h a t r e l a t e t o t h e i n t e r p r e t a t i o n o f t h e r e g u l a r e x p r e s s i o n i n s i d e a r e l i s t e d b e l o w . M o d i f i e r s t h a t a l t e r t h e w a y a r e g u l a r e x p r e s s i o n i s u s e d b y P e r l a r e d e t a i l e d i n " R e g e x p Q u o t e - L i k e O p e r a t o r s " i n p e r l o p a n d " G o r y d e t a i l s o f p a r s i n g q u o t e d c o n s t r u c t s " i n p e r l o p .
# m
T r e a t s t r i n g a s m u l t i p l e l i n e s . T h a t i s , c h a n g e " ^ " a n d " $ " f r o m m a t c h i n g t h e s t a r t o r e n d o f t h e s t r i n g t o m a t c h i n g t h e s t a r t o r e n d o f a n y l i n e a n y w h e r e w i t h i n t h e s t r i n g .
# s
T r e a t s t r i n g a s s i n g l e l i n e . T h a t i s , c h a n g e " . " t o m a t c h a n y c h a r a c t e r w h a t s o e v e r , e v e n a n e w l i n e , w h i c h n o r m a l l y i t w o u l d n o t m a t c h .
U s e d t o g e t h e r , a s / m s
, t h e y l e t t h e " . " m a t c h a n y c h a r a c t e r w h a t s o e v e r , w h i l e s t i l l a l l o w i n g " ^ " a n d " $ " t o m a t c h , r e s p e c t i v e l y , j u s t a f t e r a n d j u s t b e f o r e n e w l i n e s w i t h i n t h e s t r i n g .
# i
D o c a s e - i n s e n s i t i v e p a t t e r n m a t c h i n g .
I f l o c a l e m a t c h i n g r u l e s a r e i n e f f e c t , t h e c a s e m a p i s t a k e n f r o m t h e c u r r e n t l o c a l e f o r c o d e p o i n t s l e s s t h a n 2 5 5 , a n d f r o m U n i c o d e r u l e s f o r l a r g e r c o d e p o i n t s . H o w e v e r , m a t c h e s t h a t w o u l d c r o s s t h e U n i c o d e r u l e s / n o n - U n i c o d e r u l e s b o u n d a r y ( o r d s 2 5 5 / 2 5 6 ) w i l l n o t s u c c e e d . S e e p e r l l o c a l e .
T h e r e a r e a n u m b e r o f U n i c o d e c h a r a c t e r s t h a t m a t c h m u l t i p l e c h a r a c t e r s u n d e r / i
. F o r e x a m p l e , L A T I N S M A L L L I G A T U R E F I
s h o u l d m a t c h t h e s e q u e n c e fi
. P e r l i s n o t c u r r e n t l y a b l e t o d o t h i s w h e n t h e m u l t i p l e c h a r a c t e r s a r e i n t h e p a t t e r n a n d a r e s p l i t b e t w e e n g r o u p i n g s , o r w h e n o n e o r m o r e a r e q u a n t i f i e d . T h u s
"\N{LATIN SMALL LIGATURE FI}" =~ /fi/i; # Matches
"\N{LATIN SMALL LIGATURE FI}" =~ /[fi ][fi ]/i; # Doesn't match!
"\N{LATIN SMALL LIGATURE FI}" =~ /fi*/i; # Doesn't match!
# The below doesn't match, and it isn't clear what $1 and $2 would
# be even if it did!!
"\N{LATIN SMALL LIGATURE FI}" =~ /(f )(i )/i; # Doesn't match!
P e r l d o e s n ' t m a t c h m u l t i p l e c h a r a c t e r s i n a n i n v e r t e d b r a c k e t e d c h a r a c t e r c l a s s , w h i c h o t h e r w i s e c o u l d b e h i g h l y c o n f u s i n g . S e e " N e g a t i o n " i n p e r l r e c h a r c l a s s .
A n o t h e r b u g i n v o l v e s c h a r a c t e r c l a s s e s t h a t m a t c h b o t h a s e q u e n c e o f m u l t i p l e c h a r a c t e r s , a n d a n i n i t i a l s u b - s t r i n g o f t h a t s e q u e n c e . F o r e x a m p l e ,
/[s\xDF]/i
s h o u l d m a t c h b o t h a s i n g l e a n d a d o u b l e " s " , s i n c e \ x D F
( o n A S C I I p l a t f o r m s ) m a t c h e s " s s " . H o w e v e r , t h i s b u g ( [ p e r l # 8 9 7 7 4 ] ) c a u s e s i t t o o n l y m a t c h a s i n g l e " s " , e v e n i f t h e f i n a l l a r g e r m a t c h f a i l s , a n d m a t c h i n g t h e d o u b l e " s s " w o u l d h a v e s u c c e e d e d .
A l s o , P e r l m a t c h i n g d o e s n ' t f u l l y c o n f o r m t o t h e c u r r e n t U n i c o d e / i
r e c o m m e n d a t i o n s , w h i c h a s k t h a t t h e m a t c h i n g b e m a d e u p o n t h e N F D ( N o r m a l i z a t i o n F o r m D e c o m p o s e d ) o f t h e t e x t . H o w e v e r , U n i c o d e i s i n t h e p r o c e s s o f r e c o n s i d e r i n g a n d r e v i s i n g t h e i r r e c o m m e n d a t i o n s .
# x
E x t e n d y o u r p a t t e r n ' s l e g i b i l i t y b y p e r m i t t i n g w h i t e s p a c e a n d c o m m e n t s . D e t a i l s i n " / x "
# p
P r e s e r v e t h e s t r i n g m a t c h e d s u c h t h a t $ { ^ P R E M A T C H } , $ { ^ M A T C H } , a n d $ { ^ P O S T M A T C H } a r e a v a i l a b l e f o r u s e a f t e r m a t c h i n g .
# g a n d c
G l o b a l m a t c h i n g , a n d k e e p t h e C u r r e n t p o s i t i o n a f t e r f a i l e d m a t c h i n g . U n l i k e i , m , s a n d x , t h e s e t w o f l a g s a f f e c t t h e w a y t h e r e g e x i s u s e d r a t h e r t h a n t h e r e g e x i t s e l f . S e e " U s i n g r e g u l a r e x p r e s s i o n s i n P e r l " i n p e r l r e t u t f o r f u r t h e r e x p l a n a t i o n o f t h e g a n d c m o d i f i e r s .
# a , d , l a n d u
T h e s e m o d i f i e r s , a l l n e w i n 5 . 1 4 , a f f e c t w h i c h c h a r a c t e r - s e t s e m a n t i c s ( U n i c o d e , e t c . ) a r e u s e d , a s d e s c r i b e d b e l o w i n " C h a r a c t e r s e t m o d i f i e r s " .
R e g u l a r e x p r e s s i o n m o d i f i e r s a r e u s u a l l y w r i t t e n i n d o c u m e n t a t i o n a s e . g . , " t h e / x
m o d i f i e r " , e v e n t h o u g h t h e d e l i m i t e r i n q u e s t i o n m i g h t n o t r e a l l y b e a s l a s h . T h e m o d i f i e r s / i m s x a d l u p
m a y a l s o b e e m b e d d e d w i t h i n t h e r e g u l a r e x p r e s s i o n i t s e l f u s i n g t h e ( ? .
. . )
c o n s t r u c t , s e e " E x t e n d e d P a t t e r n s " b e l o w .
/ x
t e l l s t h e r e g u l a r e x p r e s s i o n p a r s e r t o i g n o r e m o s t w h i t e s p a c e t h a t i s n e i t h e r b a c k s l a s h e d n o r w i t h i n a c h a r a c t e r c l a s s . Y o u c a n u s e t h i s t o b r e a k u p y o u r r e g u l a r e x p r e s s i o n i n t o ( s l i g h t l y ) m o r e r e a d a b l e p a r t s . T h e #
c h a r a c t e r i s a l s o t r e a t e d a s a m e t a c h a r a c t e r i n t r o d u c i n g a c o m m e n t , j u s t a s i n o r d i n a r y P e r l c o d e . T h i s a l s o m e a n s t h a t i f y o u w a n t r e a l w h i t e s p a c e o r #
c h a r a c t e r s i n t h e p a t t e r n ( o u t s i d e a c h a r a c t e r c l a s s , w h e r e t h e y a r e u n a f f e c t e d b y / x
) , t h e n y o u ' l l e i t h e r h a v e t o e s c a p e t h e m ( u s i n g b a c k s l a s h e s o r \ Q . . . \ E
) o r e n c o d e t h e m u s i n g o c t a l , h e x , o r \ N { }
e s c a p e s . T a k e n t o g e t h e r , t h e s e f e a t u r e s g o a l o n g w a y t o w a r d s m a k i n g P e r l ' s r e g u l a r e x p r e s s i o n s m o r e r e a d a b l e . N o t e t h a t y o u h a v e t o b e c a r e f u l n o t t o i n c l u d e t h e p a t t e r n d e l i m i t e r i n t h e c o m m e n t - - p e r l h a s n o w a y o f k n o w i n g y o u d i d n o t i n t e n d t o c l o s e t h e p a t t e r n e a r l y . S e e t h e C - c o m m e n t d e l e t i o n c o d e i n p e r l o p . A l s o n o t e t h a t a n y t h i n g i n s i d e a \ Q . . . \ E
s t a y s u n a f f e c t e d b y / x
. A n d n o t e t h a t / x
d o e s n ' t a f f e c t s p a c e i n t e r p r e t a t i o n w i t h i n a s i n g l e m u l t i - c h a r a c t e r c o n s t r u c t . F o r e x a m p l e i n \ x { . . . }
, r e g a r d l e s s o f t h e / x
m o d i f i e r , t h e r e c a n b e n o s p a c e s . S a m e f o r a q u a n t i f i e r s u c h a s { 3 }
or { 5 , }
. S i m i l a r l y , ( ? : . . . )
c a n ' t h a v e a s p a c e b e t w e e n t h e ?
a n d :
, b u t c a n b e t w e e n t h e (
a n d ?
. W i t h i n a n y d e l i m i t e r s f o r s u c h a c o n s t r u c t , a l l o w e d s p a c e s a r e n o t a f f e c t e d b y / x
, a n d d e p e n d o n t h e c o n s t r u c t . F o r e x a m p l e , \ x { . . . }
c a n ' t h a v e s p a c e s b e c a u s e h e x a d e c i m a l n u m b e r s d o n ' t h a v e s p a c e s i n t h e m . B u t , U n i c o d e p r o p e r t i e s c a n h a v e s p a c e s , s o i n \ p { . . . }
t h e r e c a n b e s p a c e s t h a t f o l l o w t h e U n i c o d e r u l e s , f o r w h i c h s e e " P r o p e r t i e s a c c e s s i b l e t h r o u g h \ p { } a n d \ P { } " i n p e r l u n i p r o p s .
# C h a r a c t e r s e t m o d i f i e r s
/ d
, / u
, / a
, a n d / l
, a v a i l a b l e s t a r t i n g i n 5 . 1 4 , a r e c a l l e d t h e c h a r a c t e r s e t m o d i f i e r s ; t h e y a f f e c t t h e c h a r a c t e r s e t s e m a n t i c s u s e d f o r t h e r e g u l a r e x p r e s s i o n .
T h e / d
, / u
, a n d / l
m o d i f i e r s a r e n o t l i k e l y t o b e o f m u c h u s e t o y o u , a n d s o y o u n e e d n o t w o r r y a b o u t t h e m v e r y m u c h . T h e y e x i s t f o r P e r l ' s i n t e r n a l u s e , s o t h a t c o m p l e x r e g u l a r e x p r e s s i o n d a t a s t r u c t u r e s c a n b e a u t o m a t i c a l l y s e r i a l i z e d a n d l a t e r e x a c t l y r e c o n s t i t u t e d , i n c l u d i n g a l l t h e i r n u a n c e s . B u t , s i n c e P e r l c a n ' t k e e p a s e c r e t , a n d t h e r e m a y b e r a r e i n s t a n c e s w h e r e t h e y a r e u s e f u l , t h e y a r e d o c u m e n t e d h e r e .
T h e / a
m o d i f i e r , o n t h e o t h e r h a n d , m a y b e u s e f u l . I t s p u r p o s e i s t o a l l o w c o d e t h a t i s t o w o r k m o s t l y o n A S C I I d a t a t o n o t h a v e t o c o n c e r n i t s e l f w i t h U n i c o d e .
B r i e f l y , / l
s e t s t h e c h a r a c t e r s e t t o t h a t o f w h a t e v e r L o c a l e i s i n e f f e c t a t t h e t i m e o f t h e e x e c u t i o n o f t h e p a t t e r n m a t c h .
/ u
s e t s t h e c h a r a c t e r s e t t o U n i c o d e .
/ a
a l s o s e t s t h e c h a r a c t e r s e t t o U n i c o d e , B U T a d d s s e v e r a l r e s t r i c t i o n s f o r A S C I I - s a f e m a t c h i n g .
/ d
i s t h e o l d , p r o b l e m a t i c , p r e - 5 . 1 4 D e f a u l t c h a r a c t e r s e t b e h a v i o r . I t s o n l y u s e i s t o f o r c e t h a t o l d b e h a v i o r .
A t a n y g i v e n t i m e , e x a c t l y o n e o f t h e s e m o d i f i e r s i s i n e f f e c t . T h e i r e x i s t e n c e a l l o w s P e r l t o k e e p t h e o r i g i n a l l y c o m p i l e d b e h a v i o r o f a r e g u l a r e x p r e s s i o n , r e g a r d l e s s o f w h a t r u l e s a r e i n e f f e c t w h e n i t i s a c t u a l l y e x e c u t e d . A n d i f i t i s i n t e r p o l a t e d i n t o a l a r g e r r e g e x , t h e o r i g i n a l ' s r u l e s c o n t i n u e t o a p p l y t o i t , a n d o n l y i t .
T h e / l
a n d / u
m o d i f i e r s a r e a u t o m a t i c a l l y s e l e c t e d f o r r e g u l a r e x p r e s s i o n s c o m p i l e d w i t h i n t h e s c o p e o f v a r i o u s p r a g m a s , a n d w e r e c o m m e n d t h a t i n g e n e r a l , y o u u s e t h o s e p r a g m a s i n s t e a d o f s p e c i f y i n g t h e s e m o d i f i e r s e x p l i c i t l y . F o r o n e t h i n g , t h e m o d i f i e r s a f f e c t o n l y p a t t e r n m a t c h i n g , a n d d o n o t e x t e n d t o e v e n a n y r e p l a c e m e n t d o n e , w h e r e a s u s i n g t h e p r a g m a s g i v e c o n s i s t e n t r e s u l t s f o r a l l a p p r o p r i a t e o p e r a t i o n s w i t h i n t h e i r s c o p e s . F o r e x a m p l e ,
s/foo/\Ubar/il
w i l l m a t c h " f o o " u s i n g t h e l o c a l e ' s r u l e s f o r c a s e - i n s e n s i t i v e m a t c h i n g , b u t t h e / l
d o e s n o t a f f e c t h o w t h e \ U
o p e r a t e s . M o s t l i k e l y y o u w a n t b o t h o f t h e m t o u s e l o c a l e r u l e s . T o d o t h i s , i n s t e a d c o m p i l e t h e r e g u l a r e x p r e s s i o n w i t h i n t h e s c o p e o f u s e l o c a l
e
. T h i s b o t h i m p l i c i t l y a d d s t h e / l
a n d a p p l i e s l o c a l e r u l e s t o t h e \ U
. T h e l e s s o n i s t o u s e l o c
a l e
a n d n o t / l
e x p l i c i t l y .
S i m i l a r l y , i t w o u l d b e b e t t e r t o u s e u s e f e a t u r e ' u n i c o d e _ s t r i n g
s '
i n s t e a d o f ,
s/foo/\Lbar/iu
t o g e t U n i c o d e r u l e s , a s t h e \ L
i n t h e f o r m e r ( b u t n o t n e c e s s a r i l y t h e l a t t e r ) w o u l d a l s o u s e U n i c o d e r u l e s .
M o r e d e t a i l o n e a c h o f t h e m o d i f i e r s f o l l o w s . M o s t l i k e l y y o u d o n ' t n e e d t o k n o w t h i s d e t a i l f o r / l
, / u
, a n d / d
, a n d c a n s k i p a h e a d t o / a .
m e a n s t o u s e t h e c u r r e n t l o c a l e ' s r u l e s ( s e e p e r l l o c a l e ) w h e n p a t t e r n m a t c h i n g . F o r e x a m p l e , \ w
w i l l m a t c h t h e " w o r d " c h a r a c t e r s o f t h a t l o c a l e , a n d " / i "
c a s e - i n s e n s i t i v e m a t c h i n g w i l l m a t c h a c c o r d i n g t o t h e l o c a l e ' s c a s e f o l d i n g r u l e s . T h e l o c a l e u s e d w i l l b e t h e o n e i n e f f e c t a t t h e t i m e o f e x e c u t i o n o f t h e p a t t e r n m a t c h . T h i s m a y n o t b e t h e s a m e a s t h e c o m p i l a t i o n - t i m e l o c a l e , a n d c a n d i f f e r f r o m o n e m a t c h t o a n o t h e r i f t h e r e i s a n i n t e r v e n i n g c a l l o f t h e s e t l o c a l e ( ) f u n c t i o n .
P e r l o n l y s u p p o r t s s i n g l e - b y t e l o c a l e s . T h i s m e a n s t h a t c o d e p o i n t s a b o v e 2 5 5 a r e t r e a t e d a s U n i c o d e n o m a t t e r w h a t l o c a l e i s i n e f f e c t . U n d e r U n i c o d e r u l e s , t h e r e a r e a f e w c a s e - i n s e n s i t i v e m a t c h e s t h a t c r o s s t h e 2 5 5 / 2 5 6 b o u n d a r y . T h e s e a r e d i s a l l o w e d u n d e r / l
. F o r e x a m p l e , 0 x F F ( o n A S C I I p l a t f o r m s ) d o e s n o t c a s e l e s s l y m a t c h t h e c h a r a c t e r a t 0 x 1 7 8 , L A T I N C A P I T A L L E T T E R Y W I T H D I A E
R E S I S
, b e c a u s e 0 x F F m a y n o t b e L
A T I N S M A L L L E T T E R Y W I T H D I A E R E S
I S
i n t h e c u r r e n t l o c a l e , a n d P e r l h a s n o w a y o f k n o w i n g i f t h a t c h a r a c t e r e v e n e x i s t s i n t h e l o c a l e , m u c h l e s s w h a t c o d e p o i n t i t i s .
T h i s m o d i f i e r m a y b e s p e c i f i e d t o b e t h e d e f a u l t b y u s e l o c a l e
, b u t s e e " W h i c h c h a r a c t e r s e t m o d i f i e r i s i n e f f e c t ? " .
m e a n s t o u s e U n i c o d e r u l e s w h e n p a t t e r n m a t c h i n g . O n A S C I I p l a t f o r m s , t h i s m e a n s t h a t t h e c o d e p o i n t s b e t w e e n 1 2 8 a n d 2 5 5 t a k e o n t h e i r L a t i n - 1 ( I S O - 8 8 5 9 - 1 ) m e a n i n g s ( w h i c h a r e t h e s a m e a s U n i c o d e ' s ) . ( O t h e r w i s e P e r l c o n s i d e r s t h e i r m e a n i n g s t o b e u n d e f i n e d . ) T h u s , u n d e r t h i s m o d i f i e r , t h e A S C I I p l a t f o r m e f f e c t i v e l y b e c o m e s a U n i c o d e p l a t f o r m ; a n d h e n c e , f o r e x a m p l e , \ w
w i l l m a t c h a n y o f t h e m o r e t h a n 1 0 0 _ 0 0 0 w o r d c h a r a c t e r s i n U n i c o d e .
U n l i k e m o s t l o c a l e s , w h i c h a r e s p e c i f i c t o a l a n g u a g e a n d c o u n t r y p a i r , U n i c o d e c l a s s i f i e s a l l t h e c h a r a c t e r s t h a t a r e l e t t e r s s o m e w h e r e i n t h e w o r l d a s \ w
. F o r e x a m p l e , y o u r l o c a l e m i g h t n o t t h i n k t h a t L A T I N S M A L L L E T T E R E T
H
i s a l e t t e r ( u n l e s s y o u h a p p e n t o s p e a k I c e l a n d i c ) , b u t U n i c o d e d o e s . S i m i l a r l y , a l l t h e c h a r a c t e r s t h a t a r e d e c i m a l d i g i t s s o m e w h e r e i n t h e w o r l d w i l l m a t c h \ d
; t h i s i s h u n d r e d s , n o t 1 0 , p o s s i b l e m a t c h e s . A n d s o m e o f t h o s e d i g i t s l o o k l i k e s o m e o f t h e 1 0 A S C I I d i g i t s , b u t m e a n a d i f f e r e n t n u m b e r , s o a h u m a n c o u l d e a s i l y t h i n k a n u m b e r i s a d i f f e r e n t q u a n t i t y t h a n i t r e a l l y i s . F o r e x a m p l e , B E N G A L I D I G I T F O U R
( U + 0 9 E A ) l o o k s v e r y m u c h l i k e a n A S C I I D I G I T E I G H T
( U + 0 0 3 8 ) . A n d , \ d +
, m a y m a t c h s t r i n g s o f d i g i t s t h a t a r e a m i x t u r e f r o m d i f f e r e n t w r i t i n g s y s t e m s , c r e a t i n g a s e c u r i t y i s s u e . " n u m ( ) " i n U n i c o d e : : U C D c a n b e u s e d t o s o r t t h i s o u t . O r t h e / a
m o d i f i e r c a n b e u s e d t o f o r c e \ d
t o m a t c h j u s t t h e A S C I I 0 t h r o u g h 9 .
A l s o , u n d e r t h i s m o d i f i e r , c a s e - i n s e n s i t i v e m a t c h i n g w o r k s o n t h e f u l l s e t o f U n i c o d e c h a r a c t e r s . T h e K E L V I N S I G N
, f o r e x a m p l e m a t c h e s t h e l e t t e r s " k " a n d " K " ; a n d L A T I N S M A L L L I G A T U R E F F
m a t c h e s t h e s e q u e n c e " f f " , w h i c h , i f y o u ' r e n o t p r e p a r e d , m i g h t m a k e i t l o o k l i k e a h e x a d e c i m a l c o n s t a n t , p r e s e n t i n g a n o t h e r p o t e n t i a l s e c u r i t y i s s u e . S e e h t t p : / / u n i c o d e . o r g / r e p o r t s / t r 3 6 f o r a d e t a i l e d d i s c u s s i o n o f U n i c o d e s e c u r i t y i s s u e s .
O n t h e E B C D I C p l a t f o r m s t h a t P e r l h a n d l e s , t h e n a t i v e c h a r a c t e r s e t i s e q u i v a l e n t t o L a t i n - 1 . T h u s t h i s m o d i f i e r c h a n g e s b e h a v i o r o n l y w h e n t h e " / i "
m o d i f i e r i s a l s o s p e c i f i e d , a n d i t t u r n s o u t i t a f f e c t s o n l y t w o c h a r a c t e r s , g i v i n g t h e m f u l l U n i c o d e s e m a n t i c s : t h e M I C R O S I G N
w i l l m a t c h t h e G r e e k c a p i t a l a n d s m a l l l e t t e r s MU
, o t h e r w i s e n o t ; a n d t h e L A T
I N C A P I T A L L E T T E R S H A R P S
w i l l m a t c h a n y o f SS
, Ss
, sS
, a n d ss
, o t h e r w i s e n o t .
T h i s m o d i f i e r m a y b e s p e c i f i e d t o b e t h e d e f a u l t b y u s e f e a t u r e
' u n i c o d e _ s t r i n g s
, u s e l o c a l e ' : n
o t _ c h a r a c t e r s '
, o r u s e 5 . 0 1 2
( o r h i g h e r ) , b u t s e e " W h i c h c h a r a c t e r s e t m o d i f i e r i s i n e f f e c t ? " .
T h i s m o d i f i e r m e a n s t o u s e t h e " D e f a u l t " n a t i v e r u l e s o f t h e p l a t f o r m e x c e p t w h e n t h e r e i s c a u s e t o u s e U n i c o d e r u l e s i n s t e a d , a s f o l l o w s :
t h e t a r g e t s t r i n g i s e n c o d e d i n U T F - 8 ; o r
t h e p a t t e r n i s e n c o d e d i n U T F - 8 ; o r
t h e p a t t e r n e x p l i c i t l y m e n t i o n s a c o d e p o i n t t h a t i s a b o v e 2 5 5 ( s a y b y \ x { 1 0 0 }
) ; o r
t h e p a t t e r n u s e s a U n i c o d e n a m e ( \ N { . . . }
) ; o r
t h e p a t t e r n u s e s a U n i c o d e p r o p e r t y ( \ p { . . . }
)
A n o t h e r m n e m o n i c f o r t h i s m o d i f i e r i s " D e p e n d s " , a s t h e r u l e s a c t u a l l y u s e d d e p e n d o n v a r i o u s t h i n g s , a n d a s a r e s u l t y o u c a n g e t u n e x p e c t e d r e s u l t s . S e e " T h e " U n i c o d e B u g " " i n p e r l u n i c o d e . T h e U n i c o d e B u g h a s b e c o m e r a t h e r i n f a m o u s , l e a d i n g t o y e t a n o t h e r ( p r i n t a b l e ) n a m e f o r t h i s m o d i f i e r , " D o d g y " .
O n A S C I I p l a t f o r m s , t h e n a t i v e r u l e s a r e A S C I I , a n d o n E B C D I C p l a t f o r m s ( a t l e a s t t h e o n e s t h a t P e r l h a n d l e s ) , t h e y a r e L a t i n - 1 .
H e r e a r e s o m e e x a m p l e s o f h o w t h a t w o r k s o n a n A S C I I p l a t f o r m :
$str = "\xDF"; # $str is not in UTF-8 format.
$str =~ /^\w/; # No match, as $str isn't in UTF-8 format.
$str .= "\x{0e0b}"; # Now $str is in UTF-8 format.
$str =~ /^\w/; # Match! $str is now in UTF-8 format.
chop $str;
$str =~ /^\w/; # Still a match! $str remains in UTF-8 format.
T h i s m o d i f i e r i s a u t o m a t i c a l l y s e l e c t e d b y d e f a u l t w h e n n o n e o f t h e o t h e r s a r e , s o y e t a n o t h e r n a m e f o r i t i s " D e f a u l t " .
B e c a u s e o f t h e u n e x p e c t e d b e h a v i o r s a s s o c i a t e d w i t h t h i s m o d i f i e r , y o u p r o b a b l y s h o u l d o n l y u s e i t t o m a i n t a i n w e i r d b a c k w a r d c o m p a t i b i l i t i e s .
# / a ( a n d / a a )
T h i s m o d i f i e r s t a n d s f o r A S C I I - r e s t r i c t ( o r A S C I I - s a f e ) . T h i s m o d i f i e r , u n l i k e t h e o t h e r s , m a y b e d o u b l e d - u p t o i n c r e a s e i t s e f f e c t .
W h e n i t a p p e a r s s i n g l y , i t c a u s e s t h e s e q u e n c e s \ d
, \ s
, \ w
, a n d t h e P o s i x c h a r a c t e r c l a s s e s t o m a t c h o n l y i n t h e A S C I I r a n g e . T h e y t h u s r e v e r t t o t h e i r p r e - 5 . 6 , p r e - U n i c o d e m e a n i n g s . U n d e r / a
, \ d
a l w a y s m e a n s p r e c i s e l y t h e d i g i t s " 0 "
to " 9 "
; \ s
m e a n s t h e f i v e c h a r a c t e r s [ \ f \ n \ r \ t ]
; \ w
m e a n s t h e 6 3 c h a r a c t e r s [ A - Z a - z 0 - 9 _ ]
; a n d l i k e w i s e , a l l t h e P o s i x c l a s s e s s u c h a s [ [ : p r i n t : ] ]
m a t c h o n l y t h e a p p r o p r i a t e A S C I I - r a n g e c h a r a c t e r s .
T h i s m o d i f i e r i s u s e f u l f o r p e o p l e w h o o n l y i n c i d e n t a l l y u s e U n i c o d e , a n d w h o d o n o t w i s h t o b e b u r d e n e d w i t h i t s c o m p l e x i t i e s a n d s e c u r i t y c o n c e r n s .
W i t h / a
, o n e c a n w r i t e \ d
w i t h c o n f i d e n c e t h a t i t w i l l o n l y m a t c h A S C I I c h a r a c t e r s , a n d s h o u l d t h e n e e d a r i s e t o m a t c h b e y o n d A S C I I , y o u c a n i n s t e a d u s e \ p { D i g i
t }
( or \ p { W o r d }
f o r \ w
) . T h e r e a r e s i m i l a r \ p { . . . }
c o n s t r u c t s t h a t c a n m a t c h b e y o n d A S C I I b o t h w h i t e s p a c e ( s e e " W h i t e s p a c e " i n p e r l r e c h a r c l a s s ) , a n d P o s i x c l a s s e s ( s e e " P O S I X C h a r a c t e r C l a s s e s " i n p e r l r e c h a r c l a s s ) . T h u s , t h i s m o d i f i e r d o e s n ' t m e a n y o u c a n ' t u s e U n i c o d e , i t m e a n s t h a t t o g e t U n i c o d e m a t c h i n g y o u m u s t e x p l i c i t l y u s e a c o n s t r u c t ( \ p { }
, \
P { }
) t h a t s i g n a l s U n i c o d e .
A s y o u w o u l d e x p e c t , t h i s m o d i f i e r c a u s e s , f o r e x a m p l e , \ D
t o m e a n t h e s a m e t h i n g a s [ ^ 0 - 9 ]
; i n f a c t , a l l n o n - A S C I I c h a r a c t e r s m a t c h \ D
, \ S
, a n d \ W
. \ b
s t i l l m e a n s t o m a t c h a t t h e b o u n d a r y b e t w e e n \ w
a n d \ W
, u s i n g t h e / a
d e f i n i t i o n s o f t h e m ( s i m i l a r l y f o r \ B
) .
O t h e r w i s e , / a
b e h a v e s l i k e t h e /
u
m o d i f i e r , i n t h a t c a s e - i n s e n s i t i v e m a t c h i n g u s e s U n i c o d e s e m a n t i c s ; f o r e x a m p l e , " k " w i l l m a t c h t h e U n i c o d e \ N { K E L V I N S I G N }
u n d e r / i
m a t c h i n g , a n d c o d e p o i n t s i n t h e L a t i n 1 r a n g e , a b o v e A S C I I w i l l h a v e U n i c o d e r u l e s w h e n i t c o m e s t o c a s e - i n s e n s i t i v e m a t c h i n g .
T o f o r b i d A S C I I / n o n - A S C I I m a t c h e s ( l i k e " k " w i t h \ N { K E L V I N S I G N }
) , s p e c i f y t h e " a " t w i c e , f o r e x a m p l e / a a i
or / a i a
. ( T h e f i r s t o c c u r r e n c e o f " a " r e s t r i c t s t h e \ d
, e t c . , a n d t h e s e c o n d o c c u r r e n c e a d d s t h e / i
r e s t r i c t i o n s . ) B u t , n o t e t h a t c o d e p o i n t s o u t s i d e t h e A S C I I r a n g e w i l l u s e U n i c o d e r u l e s f o r / i
m a t c h i n g , s o t h e m o d i f i e r d o e s n ' t r e a l l y r e s t r i c t t h i n g s t o j u s t A S C I I ; i t j u s t f o r b i d s t h e i n t e r m i x i n g o f A S C I I a n d n o n - A S C I I .
T o s u m m a r i z e , t h i s m o d i f i e r p r o v i d e s p r o t e c t i o n f o r a p p l i c a t i o n s t h a t d o n ' t w i s h t o b e e x p o s e d t o a l l o f U n i c o d e . S p e c i f y i n g i t t w i c e g i v e s a d d e d p r o t e c t i o n .
T h i s m o d i f i e r m a y b e s p e c i f i e d t o b e t h e d e f a u l t b y u s e r e ' / a '
or u s e r e ' / a a '
. I f y o u d o s o , y o u m a y a c t u a l l y h a v e o c c a s i o n t o u s e t h e / u
m o d i f i e r e x p l i c t l y i f t h e r e a r e a f e w r e g u l a r e x p r e s s i o n s w h e r e y o u d o w a n t f u l l U n i c o d e r u l e s ( b u t e v e n h e r e , i t ' s b e s t i f e v e r y t h i n g w e r e u n d e r f e a t u r e " u n i c o d e _ s t r i n g s "
, a l o n g w i t h t h e u s e r e ' / a a '
) . A l s o s e e " W h i c h c h a r a c t e r s e t m o d i f i e r i s i n e f f e c t ? " .
# W h i c h c h a r a c t e r s e t m o d i f i e r i s i n e f f e c t ?
W h i c h o f t h e s e m o d i f i e r s i s i n e f f e c t a t a n y g i v e n p o i n t i n a r e g u l a r e x p r e s s i o n d e p e n d s o n a f a i r l y c o m p l e x s e t o f i n t e r a c t i o n s . T h e s e h a v e b e e n d e s i g n e d s o t h a t i n g e n e r a l y o u d o n ' t h a v e t o w o r r y a b o u t i t , b u t t h i s s e c t i o n g i v e s t h e g o r y d e t a i l s . A s e x p l a i n e d b e l o w i n " E x t e n d e d P a t t e r n s " i t i s p o s s i b l e t o e x p l i c i t l y s p e c i f y m o d i f i e r s t h a t a p p l y o n l y t o p o r t i o n s o f a r e g u l a r e x p r e s s i o n . T h e i n n e r m o s t a l w a y s h a s p r i o r i t y o v e r a n y o u t e r o n e s , a n d o n e a p p l y i n g t o t h e w h o l e e x p r e s s i o n h a s p r i o r i t y o v e r a n y o f t h e d e f a u l t s e t t i n g s t h a t a r e d e s c r i b e d i n t h e r e m a i n d e r o f t h i s s e c t i o n .
T h e u s e r e ' / f o o '
p r a g m a c a n b e u s e d t o s e t d e f a u l t m o d i f i e r s ( i n c l u d i n g t h e s e ) f o r r e g u l a r e x p r e s s i o n s c o m p i l e d w i t h i n i t s s c o p e . T h i s p r a g m a h a s p r e c e d e n c e o v e r t h e o t h e r p r a g m a s l i s t e d b e l o w t h a t a l s o c h a n g e t h e d e f a u l t s .
O t h e r w i s e , u s e l o c a l e
s e t s t h e d e f a u l t m o d i f i e r t o / l
; a n d u s e f
e a t u r e ' u n i c o d e _ s t r i n g s
, o r u s e
5 . 0 1 2
( o r h i g h e r ) s e t t h e d e f a u l t t o / u
w h e n n o t i n t h e s a m e s c o p e a s e i t h e r u s e l o c a l e
or u s e b y t e
s
. ( u s e l o c a l e ' : n o t _ c h a r a c t e r s '
a l s o s e t s t h e d e f a u l t t o / u
, o v e r r i d i n g a n y p l a i n u s e l o c a l e
. ) U n l i k e t h e m e c h a n i s m s m e n t i o n e d a b o v e , t h e s e a f f e c t o p e r a t i o n s b e s i d e s r e g u l a r e x p r e s s i o n s p a t t e r n m a t c h i n g , a n d s o g i v e m o r e c o n s i s t e n t r e s u l t s w i t h o t h e r o p e r a t o r s , i n c l u d i n g u s i n g \ U
, \ l
, e t c . i n s u b s t i t u t i o n r e p l a c e m e n t s .
I f n o n e o f t h e a b o v e a p p l y , f o r b a c k w a r d s c o m p a t i b i l i t y r e a s o n s , t h e / d
m o d i f i e r i s t h e o n e i n e f f e c t b y d e f a u l t . A s t h i s c a n l e a d t o u n e x p e c t e d r e s u l t s , i t i s b e s t t o s p e c i f y w h i c h o t h e r r u l e s e t s h o u l d b e u s e d .
# C h a r a c t e r s e t m o d i f i e r b e h a v i o r p r i o r t o P e r l 5 . 1 4
P r i o r t o 5 . 1 4 , t h e r e w e r e n o e x p l i c i t m o d i f i e r s , b u t / l
w a s i m p l i e d f o r r e g e x e s c o m p i l e d w i t h i n t h e s c o p e o f u s e l o c a l e
, a n d / d
w a s i m p l i e d o t h e r w i s e . H o w e v e r , i n t e r p o l a t i n g a r e g e x i n t o a l a r g e r r e g e x w o u l d i g n o r e t h e o r i g i n a l c o m p i l a t i o n i n f a v o r o f w h a t e v e r w a s i n e f f e c t a t t h e t i m e o f t h e s e c o n d c o m p i l a t i o n . T h e r e w e r e a n u m b e r o f i n c o n s i s t e n c i e s ( b u g s ) w i t h t h e / d
m o d i f i e r , w h e r e U n i c o d e r u l e s w o u l d b e u s e d w h e n i n a p p r o p r i a t e , a n d v i c e v e r s a . \ p { }
d i d n o t i m p l y U n i c o d e r u l e s , a n d n e i t h e r d i d a l l o c c u r r e n c e s o f \ N { }
, u n t i l 5 . 1 2 .
# R e g u l a r E x p r e s s i o n s
T h e p a t t e r n s u s e d i n P e r l p a t t e r n m a t c h i n g e v o l v e d f r o m t h o s e s u p p l i e d i n t h e V e r s i o n 8 r e g e x r o u t i n e s . ( T h e r o u t i n e s a r e d e r i v e d ( d i s t a n t l y ) f r o m H e n r y S p e n c e r ' s f r e e l y r e d i s t r i b u t a b l e r e i m p l e m e n t a t i o n o f t h e V 8 r o u t i n e s . ) S e e " V e r s i o n 8 R e g u l a r E x p r e s s i o n s " f o r d e t a i l s .
I n p a r t i c u l a r t h e f o l l o w i n g m e t a c h a r a c t e r s h a v e t h e i r s t a n d a r d e g r e p - i s h m e a n i n g s :
\ Quote the next metacharacter
^ Match the beginning of the line
. Match any character (except newline)
$ Match the end of the line (or before newline at the end)
| Alternation
() Grouping
[] Bracketed Character class
B y d e f a u l t , t h e " ^ " c h a r a c t e r i s g u a r a n t e e d t o m a t c h o n l y t h e b e g i n n i n g o f t h e s t r i n g , t h e " $ " c h a r a c t e r o n l y t h e e n d ( o r b e f o r e t h e n e w l i n e a t t h e e n d ) , a n d P e r l d o e s c e r t a i n o p t i m i z a t i o n s w i t h t h e a s s u m p t i o n t h a t t h e s t r i n g c o n t a i n s o n l y o n e l i n e . E m b e d d e d n e w l i n e s w i l l n o t b e m a t c h e d b y " ^ " o r " $ " . Y o u m a y , h o w e v e r , w i s h t o t r e a t a s t r i n g a s a m u l t i - l i n e b u f f e r , s u c h t h a t t h e " ^ " w i l l m a t c h a f t e r a n y n e w l i n e w i t h i n t h e s t r i n g ( e x c e p t i f t h e n e w l i n e i s t h e l a s t c h a r a c t e r i n t h e s t r i n g ) , a n d " $ " w i l l m a t c h b e f o r e a n y n e w l i n e . A t t h e c o s t o f a l i t t l e m o r e o v e r h e a d , y o u c a n d o t h i s b y u s i n g t h e / m m o d i f i e r o n t h e p a t t e r n m a t c h o p e r a t o r . ( O l d e r p r o g r a m s d i d t h i s b y s e t t i n g $ *
, b u t t h i s o p t i o n w a s r e m o v e d i n p e r l 5 . 9 . )
T o s i m p l i f y m u l t i - l i n e s u b s t i t u t i o n s , t h e " . " c h a r a c t e r n e v e r m a t c h e s a n e w l i n e u n l e s s y o u u s e t h e / s
m o d i f i e r , w h i c h i n e f f e c t t e l l s P e r l t o p r e t e n d t h e s t r i n g i s a s i n g l e l i n e - - e v e n i f i t i s n ' t .
# Q u a n t i f i e r s
T h e f o l l o w i n g s t a n d a r d q u a n t i f i e r s a r e r e c o g n i z e d :
* Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times
{n} Match exactly n times
{n,} Match at least n times
{n,m} Match at least n but not more than m times
( I f a c u r l y b r a c k e t o c c u r s i n a n y o t h e r c o n t e x t a n d d o e s n o t f o r m p a r t o f a b a c k s l a s h e d s e q u e n c e l i k e \ x { . . . }
, i t i s t r e a t e d a s a r e g u l a r c h a r a c t e r . I n p a r t i c u l a r , t h e l o w e r q u a n t i f i e r b o u n d i s n o t o p t i o n a l . H o w e v e r , i n P e r l v 5 . 1 8 , i t i s p l a n n e d t o i s s u e a d e p r e c a t i o n w a r n i n g f o r a l l s u c h o c c u r r e n c e s , a n d i n P e r l v 5 . 2 0 t o r e q u i r e l i t e r a l u s e s o f a c u r l y b r a c k e t t o b e e s c a p e d , s a y b y p r e c e d i n g t h e m w i t h a b a c k s l a s h o r e n c l o s i n g t h e m w i t h i n s q u a r e b r a c k e t s , ( " \ { "
or " [ { ] "
) . T h i s c h a n g e w i l l a l l o w f o r f u t u r e s y n t a x e x t e n s i o n s ( l i k e m a k i n g t h e l o w e r b o u n d o f a q u a n t i f i e r o p t i o n a l ) , a n d b e t t e r e r r o r c h e c k i n g o f q u a n t i f i e r s . N o w , a t y p o i n a q u a n t i f i e r s i l e n t l y c a u s e s i t t o b e t r e a t e d a s t h e l i t e r a l c h a r a c t e r s . F o r e x a m p l e ,
/o{4,3}/
l o o k s l i k e a q u a n t i f i e r t h a t m a t c h e s 0 t i m e s , s i n c e 4 i s g r e a t e r t h a n 3 , b u t i t r e a l l y m e a n s t o m a t c h t h e s e q u e n c e o f s i x c h a r a c t e r s " o { 4 , 3 } "
. )
T h e " * " q u a n t i f i e r i s e q u i v a l e n t t o { 0 , }
, t h e " + " q u a n t i f i e r t o { 1 , }
, a n d t h e " ? " q u a n t i f i e r t o { 0 , 1 }
. n a n d m a r e l i m i t e d t o n o n - n e g a t i v e i n t e g r a l v a l u e s l e s s t h a n a p r e s e t l i m i t d e f i n e d w h e n p e r l i s b u i l t . T h i s i s u s u a l l y 3 2 7 6 6 o n t h e m o s t c o m m o n p l a t f o r m s . T h e a c t u a l l i m i t c a n b e s e e n i n t h e e r r o r m e s s a g e g e n e r a t e d b y c o d e s u c h a s t h i s :
$_ **= $_ , / {$_} / for 2 .. 42;
B y d e f a u l t , a q u a n t i f i e d s u b p a t t e r n i s " g r e e d y " , t h a t i s , i t w i l l m a t c h a s m a n y t i m e s a s p o s s i b l e ( g i v e n a p a r t i c u l a r s t a r t i n g l o c a t i o n ) w h i l e s t i l l a l l o w i n g t h e r e s t o f t h e p a t t e r n t o m a t c h . I f y o u w a n t i t t o m a t c h t h e m i n i m u m n u m b e r o f t i m e s p o s s i b l e , f o l l o w t h e q u a n t i f i e r w i t h a " ? " . N o t e t h a t t h e m e a n i n g s d o n ' t c h a n g e , j u s t t h e " g r e e d i n e s s " :
*? Match 0 or more times, not greedily
+? Match 1 or more times, not greedily
?? Match 0 or 1 time, not greedily
{n}? Match exactly n times, not greedily (redundant)
{n,}? Match at least n times, not greedily
{n,m}? Match at least n but not more than m times, not greedily
B y d e f a u l t , w h e n a q u a n t i f i e d s u b p a t t e r n d o e s n o t a l l o w t h e r e s t o f t h e o v e r a l l p a t t e r n t o m a t c h , P e r l w i l l b a c k t r a c k . H o w e v e r , t h i s b e h a v i o u r i s s o m e t i m e s u n d e s i r a b l e . T h u s P e r l p r o v i d e s t h e " p o s s e s s i v e " q u a n t i f i e r f o r m a s w e l l .
*+ Match 0 or more times and give nothing back
++ Match 1 or more times and give nothing back
?+ Match 0 or 1 time and give nothing back
{n}+ Match exactly n times and give nothing back (redundant)
{n,}+ Match at least n times and give nothing back
{n,m}+ Match at least n but not more than m times and give nothing back
F o r i n s t a n c e ,
'aaaa' =~ /a++a/
w i l l n e v e r m a t c h , a s t h e a + +
w i l l g o b b l e u p a l l t h e a
' s i n t h e s t r i n g a n d w o n ' t l e a v e a n y f o r t h e r e m a i n i n g p a r t o f t h e p a t t e r n . T h i s f e a t u r e c a n b e e x t r e m e l y u s e f u l t o g i v e p e r l h i n t s a b o u t w h e r e i t s h o u l d n ' t b a c k t r a c k . F o r i n s t a n c e , t h e t y p i c a l " m a t c h a d o u b l e - q u o t e d s t r i n g " p r o b l e m c a n b e m o s t e f f i c i e n t l y p e r f o r m e d w h e n w r i t t e n a s :
/"(?:[^"\\]++|\\.)*+"/
a s w e k n o w t h a t i f t h e f i n a l q u o t e d o e s n o t m a t c h , b a c k t r a c k i n g w i l l n o t h e l p . S e e t h e i n d e p e n d e n t s u b e x p r e s s i o n " ( ? > p a t t e r n )
" f o r m o r e d e t a i l s ; p o s s e s s i v e q u a n t i f i e r s a r e j u s t s y n t a c t i c s u g a r f o r t h a t c o n s t r u c t . F o r i n s t a n c e t h e a b o v e e x a m p l e c o u l d a l s o b e w r i t t e n a s f o l l o w s :
/"(?>(?:(?>[^"\\]+)|\\.)*)"/
# E s c a p e s e q u e n c e s
B e c a u s e p a t t e r n s a r e p r o c e s s e d a s d o u b l e - q u o t e d s t r i n g s , t h e f o l l o w i n g a l s o w o r k :
\t tab (HT, TAB)
\n newline (LF, NL)
\r return (CR )
\f form feed (FF )
\a alarm (bell) (BEL)
\e escape (think troff) (ESC)
\cK control char (example: VT)
\x{}, \x00 character whose ordinal is the given hexadecimal number
\N{name} named Unicode character or character sequence
\N{U+263D} Unicode character (example: FIRST QUARTER MOON)
\o{}, \000 character whose ordinal is the given octal number
\l lowercase next char (think vi)
\u uppercase next char (think vi)
\L lowercase till \E (think vi)
\U uppercase till \E (think vi)
\Q quote (disable) pattern metacharacters till \E
\E end either case modification or quoted section, think vi
D e t a i l s a r e i n " Q u o t e a n d Q u o t e - l i k e O p e r a t o r s " i n p e r l o p .
# C h a r a c t e r C l a s s e s a n d o t h e r S p e c i a l E s c a p e s
I n a d d i t i o n , P e r l d e f i n e s t h e f o l l o w i n g :
Sequence Note Description
[...] [1 ] Match a character according to the rules of the
bracketed character class defined by the "...".
Example: [a-z] matches "a" or "b" or "c" ... or "z"
[[:...:]] [2 ] Match a character according to the rules of the POSIX
character class "..." within the outer bracketed
character class. Example: [[:upper:]] matches any
uppercase character.
\w [3 ] Match a "word" character (alphanumeric plus "_", plus
other connector punctuation chars plus Unicode
marks)
\W [3 ] Match a non-"word" character
\s [3 ] Match a whitespace character
\S [3 ] Match a non-whitespace character
\d [3 ] Match a decimal digit character
\D [3 ] Match a non-digit character
\pP [3 ] Match P, named property. Use \p{Prop} for longer names
\PP [3 ] Match non-P
\X [4 ] Match Unicode "eXtended grapheme cluster"
\C Match a single C-language char (octet) even if that is
part of a larger UTF-8 character. Thus it breaks up
characters into their UTF-8 bytes, so you may end up
with malformed pieces of UTF-8. Unsupported in
lookbehind.
\1 [5 ] Backreference to a specific capture group or buffer.
'1' may actually be any positive integer.
\g1 [5 ] Backreference to a specific or previous group,
\g{-1} [5 ] The number may be negative indicating a relative
previous group and may optionally be wrapped in
curly brackets for safer parsing.
\g{name} [5 ] Named backreference
\k<name> [5 ] Named backreference
\K [6 ] Keep the stuff left of the \K, don't include it in $&
\N [7 ] Any character but \n (experimental). Not affected by
/s modifier
\v [3 ] Vertical whitespace
\V [3 ] Not vertical whitespace
\h [3 ] Horizontal whitespace
\H [3 ] Not horizontal whitespace
\R [4 ] Linebreak
# [ 1 ]
S e e " B r a c k e t e d C h a r a c t e r C l a s s e s " i n p e r l r e c h a r c l a s s f o r d e t a i l s .
# [ 2 ]
S e e " P O S I X C h a r a c t e r C l a s s e s " i n p e r l r e c h a r c l a s s f o r d e t a i l s .
# [ 3 ]
S e e " B a c k s l a s h s e q u e n c e s " i n p e r l r e c h a r c l a s s f o r d e t a i l s .
# [ 4 ]
S e e " M i s c " i n p e r l r e b a c k s l a s h f o r d e t a i l s .
# [ 5 ]
S e e " C a p t u r e g r o u p s " b e l o w f o r d e t a i l s .
# [ 6 ]
S e e " E x t e n d e d P a t t e r n s " b e l o w f o r d e t a i l s .
# [ 7 ]
N o t e t h a t \ N
h a s t w o m e a n i n g s . W h e n o f t h e f o r m \ N { N A M E }
, i t m a t c h e s t h e c h a r a c t e r o r c h a r a c t e r s e q u e n c e w h o s e n a m e i s N A M E
; a n d s i m i l a r l y w h e n o f t h e f o r m \ N { U + h e x }
, i t m a t c h e s t h e c h a r a c t e r w h o s e U n i c o d e c o d e p o i n t i s h e x . O t h e r w i s e i t m a t c h e s a n y c h a r a c t e r b u t \ n
.
# A s s e r t i o n s
P e r l d e f i n e s t h e f o l l o w i n g z e r o - w i d t h a s s e r t i o n s :
\b Match a word boundary
\B Match except at a word boundary
\A Match only at beginning of string
\Z Match only at end of string, or before newline at the end
\z Match only at end of string
\G Match only at pos() (e.g. at the end-of-match position
of prior m//g)
A w o r d b o u n d a r y ( \ b
) i s a s p o t b e t w e e n t w o c h a r a c t e r s t h a t h a s a \ w
o n o n e s i d e o f i t a n d a \ W
o n t h e o t h e r s i d e o f i t ( i n e i t h e r o r d e r ) , c o u n t i n g t h e i m a g i n a r y c h a r a c t e r s o f f t h e b e g i n n i n g a n d e n d o f t h e s t r i n g a s m a t c h i n g a \ W
. ( W i t h i n c h a r a c t e r c l a s s e s \ b
r e p r e s e n t s b a c k s p a c e r a t h e r t h a n a w o r d b o u n d a r y , j u s t a s i t n o r m a l l y d o e s i n a n y d o u b l e - q u o t e d s t r i n g . ) T h e \ A
a n d \ Z
a r e j u s t l i k e " ^ " a n d " $ " , e x c e p t t h a t t h e y w o n ' t m a t c h m u l t i p l e t i m e s w h e n t h e / m
m o d i f i e r i s u s e d , w h i l e " ^ " a n d " $ " w i l l m a t c h a t e v e r y i n t e r n a l l i n e b o u n d a r y . T o m a t c h t h e a c t u a l e n d o f t h e s t r i n g a n d n o t i g n o r e a n o p t i o n a l t r a i l i n g n e w l i n e , u s e \ z
.
T h e \ G
a s s e r t i o n c a n b e u s e d t o c h a i n g l o b a l m a t c h e s ( u s i n g m / / g
) , a s d e s c r i b e d i n " R e g e x p Q u o t e - L i k e O p e r a t o r s " i n p e r l o p . I t i s a l s o u s e f u l w h e n w r i t i n g l e x
- l i k e s c a n n e r s , w h e n y o u h a v e s e v e r a l p a t t e r n s t h a t y o u w a n t t o m a t c h a g a i n s t c o n s e q u e n t s u b s t r i n g s o f y o u r s t r i n g ; s e e t h e p r e v i o u s r e f e r e n c e . T h e a c t u a l l o c a t i o n w h e r e \ G
w i l l m a t c h c a n a l s o b e i n f l u e n c e d b y u s i n g p o s ( )
a s a n l v a l u e : s e e " p o s " i n p e r l f u n c . N o t e t h a t t h e r u l e f o r z e r o - l e n g t h m a t c h e s ( s e e " R e p e a t e d P a t t e r n s M a t c h i n g a Z e r o - l e n g t h S u b s t r i n g " ) i s m o d i f i e d s o m e w h a t , i n t h a t c o n t e n t s t o t h e l e f t o f \ G
a r e n o t c o u n t e d w h e n d e t e r m i n i n g t h e l e n g t h o f t h e m a t c h . T h u s t h e f o l l o w i n g w i l l n o t m a t c h f o r e v e r :
my $string = 'ABC';
pos($string) = 1;
while ($string =~ /(.\G)/g) {
print $1;
}
I t w i l l p r i n t ' A ' a n d t h e n t e r m i n a t e , a s i t c o n s i d e r s t h e m a t c h t o b e z e r o - w i d t h , a n d t h u s w i l l n o t m a t c h a t t h e s a m e p o s i t i o n t w i c e i n a r o w .
I t i s w o r t h n o t i n g t h a t \ G
i m p r o p e r l y u s e d c a n r e s u l t i n a n i n f i n i t e l o o p . T a k e c a r e w h e n u s i n g p a t t e r n s t h a t i n c l u d e \ G
i n a n a l t e r n a t i o n .
# C a p t u r e g r o u p s
T h e b r a c k e t i n g c o n s t r u c t ( . . . )
c r e a t e s c a p t u r e g r o u p s ( a l s o r e f e r r e d t o a s c a p t u r e b u f f e r s ) . T o r e f e r t o t h e c u r r e n t c o n t e n t s o f a g r o u p l a t e r o n , w i t h i n t h e s a m e p a t t e r n , u s e \ g 1
( or \ g { 1 }
) f o r t h e f i r s t , \ g 2
( or \ g { 2 }
) f o r t h e s e c o n d , a n d s o o n . T h i s i s c a l l e d a b a c k r e f e r e n c e . T h e r e i s n o l i m i t t o t h e n u m b e r o f c a p t u r e d s u b s t r i n g s t h a t y o u m a y u s e . G r o u p s a r e n u m b e r e d w i t h t h e l e f t m o s t o p e n p a r e n t h e s i s b e i n g n u m b e r 1 , e t c . I f a g r o u p d i d n o t m a t c h , t h e a s s o c i a t e d b a c k r e f e r e n c e w o n ' t m a t c h e i t h e r . ( T h i s c a n h a p p e n i f t h e g r o u p i s o p t i o n a l , o r i n a d i f f e r e n t b r a n c h o f a n a l t e r n a t i o n . ) Y o u c a n o m i t t h e "
g "
, a n d w r i t e " \ 1 "
, e t c , b u t t h e r e a r e s o m e i s s u e s w i t h t h i s f o r m , d e s c r i b e d b e l o w .
Y o u c a n a l s o r e f e r t o c a p t u r e g r o u p s r e l a t i v e l y , b y u s i n g a n e g a t i v e n u m b e r , s o t h a t \ g - 1
a n d \ g
{ - 1 }
b o t h r e f e r t o t h e i m m e d i a t e l y p r e c e d i n g c a p t u r e g r o u p , a n d \ g - 2
a n d \ g { - 2 }
b o t h r e f e r t o t h e g r o u p b e f o r e i t . F o r e x a m p l e :
/
(Y ) # group 1
( # group 2
(X ) # group 3
\g{-1} # backref to group 3
\g{-3} # backref to group 1
)
/x
w o u l d m a t c h t h e s a m e a s / ( Y ) ( (
X ) \ g 3 \ g 1 ) / x
. T h i s a l l o w s y o u t o i n t e r p o l a t e r e g e x e s i n t o l a r g e r r e g e x e s a n d n o t h a v e t o w o r r y a b o u t t h e c a p t u r e g r o u p s b e i n g r e n u m b e r e d .
Y o u c a n d i s p e n s e w i t h n u m b e r s a l t o g e t h e r a n d c r e a t e n a m e d c a p t u r e g r o u p s . T h e n o t a t i o n i s ( ? < n a m
e > . . . )
t o d e c l a r e a n d \ g { n a m e }
t o r e f e r e n c e . ( T o b e c o m p a t i b l e w i t h . N e t r e g u l a r e x p r e s s i o n s , \ g
{ n a m e }
m a y a l s o b e w r i t t e n a s \ k
{ n a m e }
, \ k < n a m e >
or \ k ' n a m e '
. ) n a m e m u s t n o t b e g i n w i t h a n u m b e r , n o r c o n t a i n h y p h e n s . W h e n d i f f e r e n t g r o u p s w i t h i n t h e s a m e p a t t e r n h a v e t h e s a m e n a m e , a n y r e f e r e n c e t o t h a t n a m e a s s u m e s t h e l e f t m o s t d e f i n e d g r o u p . N a m e d g r o u p s c o u n t i n a b s o l u t e a n d r e l a t i v e n u m b e r i n g , a n d s o c a n a l s o b e r e f e r r e d t o b y t h o s e n u m b e r s . ( I t ' s p o s s i b l e t o d o t h i n g s w i t h n a m e d c a p t u r e g r o u p s t h a t w o u l d o t h e r w i s e r e q u i r e ( ?? { } )
. )
C a p t u r e g r o u p c o n t e n t s a r e d y n a m i c a l l y s c o p e d a n d a v a i l a b l e t o y o u o u t s i d e t h e p a t t e r n u n t i l t h e e n d o f t h e e n c l o s i n g b l o c k o r u n t i l t h e n e x t s u c c e s s f u l m a t c h , w h i c h e v e r c o m e s f i r s t . ( S e e " C o m p o u n d S t a t e m e n t s " i n p e r l s y n . ) Y o u c a n r e f e r t o t h e m b y a b s o l u t e n u m b e r ( u s i n g " $ 1 "
i n s t e a d o f "
\ g 1 "
, e t c ) ; o r b y n a m e v i a t h e %
+
h a s h , u s i n g " $ + { n a m e } "
.
B r a c e s a r e r e q u i r e d i n r e f e r r i n g t o n a m e d c a p t u r e g r o u p s , b u t a r e o p t i o n a l f o r a b s o l u t e o r r e l a t i v e n u m b e r e d o n e s . B r a c e s a r e s a f e r w h e n c r e a t i n g a r e g e x b y c o n c a t e n a t i n g s m a l l e r s t r i n g s . F o r e x a m p l e i f y o u h a v e q r / $ a $ b /
, a n d $ a
c o n t a i n e d " \ g 1 "
, a n d $ b
c o n t a i n e d " 3 7 "
, y o u w o u l d g e t / \ g 1 3
7 /
w h i c h i s p r o b a b l y n o t w h a t y o u i n t e n d e d .
T h e \ g
a n d \ k
n o t a t i o n s w e r e i n t r o d u c e d i n P e r l 5 . 1 0 . 0 . P r i o r t o t h a t t h e r e w e r e n o n a m e d n o r r e l a t i v e n u m b e r e d c a p t u r e g r o u p s . A b s o l u t e n u m b e r e d g r o u p s w e r e r e f e r r e d t o u s i n g \ 1
, \ 2
, e t c . , a n d t h i s n o t a t i o n i s s t i l l a c c e p t e d ( a n d l i k e l y a l w a y s w i l l b e ) . B u t i t l e a d s t o s o m e a m b i g u i t i e s i f t h e r e a r e m o r e t h a n 9 c a p t u r e g r o u p s , a s \ 1 0
c o u l d m e a n e i t h e r t h e t e n t h c a p t u r e g r o u p , o r t h e c h a r a c t e r w h o s e o r d i n a l i n o c t a l i s 0 1 0 ( a b a c k s p a c e i n A S C I I ) . P e r l r e s o l v e s t h i s a m b i g u i t y b y i n t e r p r e t i n g \ 1 0
a s a b a c k r e f e r e n c e o n l y i f a t l e a s t 1 0 l e f t p a r e n t h e s e s h a v e o p e n e d b e f o r e i t . L i k e w i s e \ 1 1
i s a b a c k r e f e r e n c e o n l y i f a t l e a s t 1 1 l e f t p a r e n t h e s e s h a v e o p e n e d b e f o r e i t . A n d s o o n . \ 1
t h r o u g h \ 9
a r e a l w a y s i n t e r p r e t e d a s b a c k r e f e r e n c e s . T h e r e a r e s e v e r a l e x a m p l e s b e l o w t h a t i l l u s t r a t e t h e s e p e r i l s . Y o u c a n a v o i d t h e a m b i g u i t y b y a l w a y s u s i n g \ g { }
or \ g
i f y o u m e a n c a p t u r i n g g r o u p s ; a n d f o r o c t a l c o n s t a n t s a l w a y s u s i n g \ o { }
, o r f o r \ 0 7 7
a n d b e l o w , u s i n g 3 d i g i t s p a d d e d w i t h l e a d i n g z e r o s , s i n c e a l e a d i n g z e r o i m p l i e s a n o c t a l c o n s t a n t .
T h e \ d i g i t
n o t a t i o n a l s o w o r k s i n c e r t a i n c i r c u m s t a n c e s o u t s i d e t h e p a t t e r n . S e e " W a r n i n g o n \ 1 I n s t e a d o f $ 1 " b e l o w f o r d e t a i l s .
E x a m p l e s :
s/^([^ ]*) *([^ ]*)/$2 $1/; # swap first two words
/(.)\g1/ # find first doubled char
and print "'$1' is the first doubled character\n";
/(?<char>.)\k<char>/ # ... a different way
and print "'$+{char}' is the first doubled character\n";
/(?'char'.)\g1/ # ... mix and match
and print "'$1' is the first doubled character\n";
if (/Time: (..):(..):(..)/) { # parse out values
$hours = $1;
$minutes = $2;
$seconds = $3;
}
/(.)(.)(.)(.)(.)(.)(.)(.)(.)\g10/ # \g10 is a backreference
/(.)(.)(.)(.)(.)(.)(.)(.)(.)\10/ # \10 is octal
/((.)(.)(.)(.)(.)(.)(.)(.)(.))\10/ # \10 is a backreference
/((.)(.)(.)(.)(.)(.)(.)(.)(.))\010/ # \010 is octal
$a = '(.)\1'; # Creates problems when concatenated.
$b = '(.)\g{1}'; # Avoids the problems.
"aa" =~ /${a}/; # True
"aa" =~ /${b}/; # True
"aa0" =~ /${a}0/; # False!
"aa0" =~ /${b}0/; # True
"aa\x08" =~ /${a}0/; # True!
"aa\x08" =~ /${b}0/; # False
S e v e r a l s p e c i a l v a r i a b l e s a l s o r e f e r b a c k t o p o r t i o n s o f t h e p r e v i o u s m a t c h . $ +
r e t u r n s w h a t e v e r t h e l a s t b r a c k e t m a t c h m a t c h e d . $ &
r e t u r n s t h e e n t i r e m a t c h e d s t r i n g . ( A t o n e p o i n t $ 0
d i d a l s o , b u t n o w i t r e t u r n s t h e n a m e o f t h e p r o g r a m . ) $ `
r e t u r n s e v e r y t h i n g b e f o r e t h e m a t c h e d s t r i n g . $
'
r e t u r n s e v e r y t h i n g a f t e r t h e m a t c h e d s t r i n g . A n d $ ^ N
c o n t a i n s w h a t e v e r w a s m a t c h e d b y t h e m o s t - r e c e n t l y c l o s e d g r o u p ( s u b m a t c h ) . $ ^ N
c a n b e u s e d i n e x t e n d e d p a t t e r n s ( s e e b e l o w ) , f o r e x a m p l e t o a s s i g n a s u b m a t c h t o a v a r i a b l e .
T h e s e s p e c i a l v a r i a b l e s , l i k e t h e % +
h a s h a n d t h e n u m b e r e d m a t c h v a r i a b l e s ( $ 1
, $ 2
, $ 3
, e t c . ) a r e d y n a m i c a l l y s c o p e d u n t i l t h e e n d o f t h e e n c l o s i n g b l o c k o r u n t i l t h e n e x t s u c c e s s f u l m a t c h , w h i c h e v e r c o m e s f i r s t . ( S e e " C o m p o u n d S t a t e m e n t s " i n p e r l s y n . )
N O T E : F a i l e d m a t c h e s i n P e r l d o n o t r e s e t t h e m a t c h v a r i a b l e s , w h i c h m a k e s i t e a s i e r t o w r i t e c o d e t h a t t e s t s f o r a s e r i e s o f m o r e s p e c i f i c c a s e s a n d r e m e m b e r s t h e b e s t m a t c h .
W A R N I N G : O n c e P e r l s e e s t h a t y o u n e e d o n e o f $ &
, $ `
, o r $ '
a n y w h e r e i n t h e p r o g r a m , i t h a s t o p r o v i d e t h e m f o r e v e r y p a t t e r n m a t c h . T h i s m a y s u b s t a n t i a l l y s l o w y o u r p r o g r a m . P e r l u s e s t h e s a m e m e c h a n i s m t o p r o d u c e $ 1
, $ 2
, e t c , s o y o u a l s o p a y a p r i c e f o r e a c h p a t t e r n t h a t c o n t a i n s c a p t u r i n g p a r e n t h e s e s . ( T o a v o i d t h i s c o s t w h i l e r e t a i n i n g t h e g r o u p i n g b e h a v i o u r , u s e t h e e x t e n d e d r e g u l a r e x p r e s s i o n ( ? : . . . )
i n s t e a d . ) B u t i f y o u n e v e r u s e $ &
, $ `
or $ '
, t h e n p a t t e r n s w i t h o u t c a p t u r i n g p a r e n t h e s e s w i l l n o t b e p e n a l i z e d . S o a v o i d $ &
, $ '
, a n d $ `
i f y o u c a n , b u t i f y o u c a n ' t ( a n d s o m e a l g o r i t h m s r e a l l y a p p r e c i a t e t h e m ) , o n c e y o u ' v e u s e d t h e m o n c e , u s e t h e m a t w i l l , b e c a u s e y o u ' v e a l r e a d y p a i d t h e p r i c e . A s o f 5 . 0 0 5 , $ &
i s n o t s o c o s t l y a s t h e o t h e r t w o .
A s a w o r k a r o u n d f o r t h i s p r o b l e m , P e r l 5 . 1 0 . 0 i n t r o d u c e s $ { ^ P R E M A
T C H }
, $ { ^ M A T C H }
a n d $ { ^ P O S T M A T C H }
, w h i c h a r e e q u i v a l e n t t o $ `
, $ &
a n d $ '
, e x c e p t t h a t t h e y a r e o n l y g u a r a n t e e d t o b e d e f i n e d a f t e r a s u c c e s s f u l m a t c h t h a t w a s e x e c u t e d w i t h t h e / p
( p r e s e r v e ) m o d i f i e r . T h e u s e o f t h e s e v a r i a b l e s i n c u r s n o g l o b a l p e r f o r m a n c e p e n a l t y , u n l i k e t h e i r p u n c t u a t i o n c h a r e q u i v a l e n t s , h o w e v e r a t t h e t r a d e - o f f t h a t y o u h a v e t o t e l l p e r l w h e n y o u w a n t t o u s e t h e m .
B a c k s l a s h e d m e t a c h a r a c t e r s i n P e r l a r e a l p h a n u m e r i c , s u c h a s \ b
, \ w
, \ n
. U n l i k e s o m e o t h e r r e g u l a r e x p r e s s i o n l a n g u a g e s , t h e r e a r e n o b a c k s l a s h e d s y m b o l s t h a t a r e n ' t a l p h a n u m e r i c . S o a n y t h i n g t h a t l o o k s l i k e \ \ , \ ( , \ ) , \ < , \ > , \ { , o r \ } i s a l w a y s i n t e r p r e t e d a s a l i t e r a l c h a r a c t e r , n o t a m e t a c h a r a c t e r . T h i s w a s o n c e u s e d i n a c o m m o n i d i o m t o d i s a b l e o r q u o t e t h e s p e c i a l m e a n i n g s o f r e g u l a r e x p r e s s i o n m e t a c h a r a c t e r s i n a s t r i n g t h a t y o u w a n t t o u s e f o r a p a t t e r n . S i m p l y q u o t e a l l n o n - " w o r d " c h a r a c t e r s :
$pattern =~ s/(\W)/\\$1/g;
( If u s e l o c a l e
i s s e t , t h e n t h i s d e p e n d s o n t h e c u r r e n t l o c a l e . ) T o d a y i t i s m o r e c o m m o n t o u s e t h e q u o t e m e t a ( ) f u n c t i o n o r t h e \ Q
m e t a q u o t i n g e s c a p e s e q u e n c e t o d i s a b l e a l l m e t a c h a r a c t e r s ' s p e c i a l m e a n i n g s l i k e t h i s :
/$unquoted\Q$quoted\E$unquoted/
B e w a r e t h a t i f y o u p u t l i t e r a l b a c k s l a s h e s ( t h o s e n o t i n s i d e i n t e r p o l a t e d v a r i a b l e s ) b e t w e e n \ Q
a n d \ E
, d o u b l e - q u o t i s h b a c k s l a s h i n t e r p o l a t i o n m a y l e a d t o c o n f u s i n g r e s u l t s . I f y o u n e e d t o u s e l i t e r a l b a c k s l a s h e s w i t h i n \ Q . . .
\ E
, c o n s u l t " G o r y d e t a i l s o f p a r s i n g q u o t e d c o n s t r u c t s " i n p e r l o p .
q u o t e m e t a ( )
a n d \ Q
a r e f u l l y d e s c r i b e d i n " q u o t e m e t a " i n p e r l f u n c .
# E x t e n d e d P a t t e r n s
P e r l a l s o d e f i n e s a c o n s i s t e n t e x t e n s i o n s y n t a x f o r f e a t u r e s n o t f o u n d i n s t a n d a r d t o o l s l i k e a w k a n d l e x . T h e s y n t a x f o r m o s t o f t h e s e i s a p a i r o f p a r e n t h e s e s w i t h a q u e s t i o n m a r k a s t h e f i r s t t h i n g w i t h i n t h e p a r e n t h e s e s . T h e c h a r a c t e r a f t e r t h e q u e s t i o n m a r k i n d i c a t e s t h e e x t e n s i o n .
T h e s t a b i l i t y o f t h e s e e x t e n s i o n s v a r i e s w i d e l y . S o m e h a v e b e e n p a r t o f t h e c o r e l a n g u a g e f o r m a n y y e a r s . O t h e r s a r e e x p e r i m e n t a l a n d m a y c h a n g e w i t h o u t w a r n i n g o r b e c o m p l e t e l y r e m o v e d . C h e c k t h e d o c u m e n t a t i o n o n a n i n d i v i d u a l f e a t u r e t o v e r i f y i t s c u r r e n t s t a t u s .
A q u e s t i o n m a r k w a s c h o s e n f o r t h i s a n d f o r t h e m i n i m a l - m a t c h i n g c o n s t r u c t b e c a u s e 1 ) q u e s t i o n m a r k s a r e r a r e i n o l d e r r e g u l a r e x p r e s s i o n s , a n d 2 ) w h e n e v e r y o u s e e o n e , y o u s h o u l d s t o p a n d " q u e s t i o n " e x a c t l y w h a t i s g o i n g o n . T h a t ' s p s y c h o l o g y . . . .
# ( ? # t e x t )
A c o m m e n t . T h e t e x t i s i g n o r e d . I f t h e / x
m o d i f i e r e n a b l e s w h i t e s p a c e f o r m a t t i n g , a s i m p l e #
w i l l s u f f i c e . N o t e t h a t P e r l c l o s e s t h e c o m m e n t a s s o o n a s i t s e e s a )
, s o t h e r e i s n o w a y t o p u t a l i t e r a l )
i n t h e c o m m e n t .
# ( ? a d l u p i m s x - i m s x )
# ( ? ^ a l u p i m s x )
O n e o r m o r e e m b e d d e d p a t t e r n - m a t c h m o d i f i e r s , t o b e t u r n e d o n ( o r t u r n e d o f f , i f p r e c e d e d b y -
) f o r t h e r e m a i n d e r o f t h e p a t t e r n o r t h e r e m a i n d e r o f t h e e n c l o s i n g p a t t e r n g r o u p ( i f a n y ) .
T h i s i s p a r t i c u l a r l y u s e f u l f o r d y n a m i c p a t t e r n s , s u c h a s t h o s e r e a d i n f r o m a c o n f i g u r a t i o n f i l e , t a k e n f r o m a n a r g u m e n t , o r s p e c i f i e d i n a t a b l e s o m e w h e r e . C o n s i d e r t h e c a s e w h e r e s o m e p a t t e r n s w a n t t o b e c a s e - s e n s i t i v e a n d s o m e d o n o t : T h e c a s e - i n s e n s i t i v e o n e s m e r e l y n e e d t o i n c l u d e ( ? i )
a t t h e f r o n t o f t h e p a t t e r n . F o r e x a m p l e :
$pattern = "foobar";
if ( /$pattern/i ) { }
# more flexible:
$pattern = "(?i)foobar";
if ( /$pattern/ ) { }
T h e s e m o d i f i e r s a r e r e s t o r e d a t t h e e n d o f t h e e n c l o s i n g g r o u p . F o r e x a m p l e ,
( (?i) blah ) \s+ \g1
w i l l m a t c h b l a h
i n a n y c a s e , s o m e s p a c e s , a n d a n e x a c t ( i n c l u d i n g t h e c a s e ! ) r e p e t i t i o n o f t h e p r e v i o u s w o r d , a s s u m i n g t h e /
x
m o d i f i e r , a n d n o / i
m o d i f i e r o u t s i d e t h i s g r o u p .
T h e s e m o d i f i e r s d o n o t c a r r y o v e r i n t o n a m e d s u b p a t t e r n s c a l l e d i n t h e e n c l o s i n g g r o u p . I n o t h e r w o r d s , a p a t t e r n s u c h a s ( (
? i ) ( ? & N A M E ) )
d o e s n o t c h a n g e t h e c a s e - s e n s i t i v i t y o f t h e " N A M E " p a t t e r n .
A n y o f t h e s e m o d i f i e r s c a n b e s e t t o a p p l y g l o b a l l y t o a l l r e g u l a r e x p r e s s i o n s c o m p i l e d w i t h i n t h e s c o p e o f a u s e r e
. S e e " ' / f l a g s ' m o d e " i n r e .
S t a r t i n g i n P e r l 5 . 1 4 , a " ^ "
( c a r e t o r c i r c u m f l e x a c c e n t ) i m m e d i a t e l y a f t e r t h e " ? "
i s a s h o r t h a n d e q u i v a l e n t t o d - i m s x
. F l a g s ( e x c e p t " d "
) m a y f o l l o w t h e c a r e t t o o v e r r i d e i t . B u t a m i n u s s i g n i s n o t l e g a l w i t h i t .
N o t e t h a t t h e a
, d
, l
, p
, a n d u
m o d i f i e r s a r e s p e c i a l i n t h a t t h e y c a n o n l y b e e n a b l e d , n o t d i s a b l e d , a n d t h e a
, d
, l
, a n d u
m o d i f i e r s a r e m u t u a l l y e x c l u s i v e : s p e c i f y i n g o n e d e - s p e c i f i e s t h e o t h e r s , a n d a m a x i m u m o f o n e ( o r t w o a
' s ) m a y a p p e a r i n t h e c o n s t r u c t . T h u s , f o r e x a m p l e , ( ? - p )
w i l l w a r n w h e n c o m p i l e d u n d e r u s e w a r n i n g s
; ( ? - d : . . . )
a n d ( ? d l : . . . )
a r e f a t a l e r r o r s .
N o t e a l s o t h a t t h e p
m o d i f i e r i s s p e c i a l i n t h a t i t s p r e s e n c e a n y w h e r e i n a p a t t e r n h a s a g l o b a l e f f e c t .
# ( ? : p a t t e r n )
# ( ? a d l u i m s x - i m s x : p a t t e r n )
# ( ? ^ a l u i m s x : p a t t e r n )
T h i s i s f o r c l u s t e r i n g , n o t c a p t u r i n g ; i t g r o u p s s u b e x p r e s s i o n s l i k e " ( ) " , b u t d o e s n ' t m a k e b a c k r e f e r e n c e s a s " ( ) " d o e s . S o
@fields = split(/\b(?:a|b|c)\b/)
i s l i k e
@fields = split(/\b(a|b|c)\b/)
b u t d o e s n ' t s p i t o u t e x t r a f i e l d s . I t ' s a l s o c h e a p e r n o t t o c a p t u r e c h a r a c t e r s i f y o u d o n ' t n e e d t o .
A n y l e t t e r s b e t w e e n ?
a n d :
a c t a s f l a g s m o d i f i e r s a s w i t h ( ? a d
l u i m s x - i m s x )
. F o r e x a m p l e ,
/(?s-i:more.*than).*million/i
i s e q u i v a l e n t t o t h e m o r e v e r b o s e
/(?:(?s-i)more.*than).*million/i
S t a r t i n g i n P e r l 5 . 1 4 , a " ^ "
( c a r e t o r c i r c u m f l e x a c c e n t ) i m m e d i a t e l y a f t e r t h e " ? "
i s a s h o r t h a n d e q u i v a l e n t t o d - i m s x
. A n y p o s i t i v e f l a g s ( e x c e p t " d "
) m a y f o l l o w t h e c a r e t , s o
(?^x:foo)
i s e q u i v a l e n t t o
(?x-ims:foo)
T h e c a r e t t e l l s P e r l t h a t t h i s c l u s t e r d o e s n ' t i n h e r i t t h e f l a g s o f a n y s u r r o u n d i n g p a t t e r n , b u t u s e s t h e s y s t e m d e f a u l t s ( d
- i m s x
) , m o d i f i e d b y a n y f l a g s s p e c i f i e d .
T h e c a r e t a l l o w s f o r s i m p l e r s t r i n g i f i c a t i o n o f c o m p i l e d r e g u l a r e x p r e s s i o n s . T h e s e l o o k l i k e
(?^:pattern)
w i t h a n y n o n - d e f a u l t f l a g s a p p e a r i n g b e t w e e n t h e c a r e t a n d t h e c o l o n . A t e s t t h a t l o o k s a t s u c h s t r i n g i f i c a t i o n t h u s d o e s n ' t n e e d t o h a v e t h e s y s t e m d e f a u l t f l a g s h a r d - c o d e d i n i t , j u s t t h e c a r e t . I f n e w f l a g s a r e a d d e d t o P e r l , t h e m e a n i n g o f t h e c a r e t ' s e x p a n s i o n w i l l c h a n g e t o i n c l u d e t h e d e f a u l t f o r t h o s e f l a g s , s o t h e t e s t w i l l s t i l l w o r k , u n c h a n g e d .
S p e c i f y i n g a n e g a t i v e f l a g a f t e r t h e c a r e t i s a n e r r o r , a s t h e f l a g i s r e d u n d a n t .
M n e m o n i c f o r ( ? ^ . . . )
: A f r e s h b e g i n n i n g s i n c e t h e u s u a l u s e o f a c a r e t i s t o m a t c h a t t h e b e g i n n i n g .
# ( ? | p a t t e r n )
T h i s i s t h e " b r a n c h r e s e t " p a t t e r n , w h i c h h a s t h e s p e c i a l p r o p e r t y t h a t t h e c a p t u r e g r o u p s a r e n u m b e r e d f r o m t h e s a m e s t a r t i n g p o i n t i n e a c h a l t e r n a t i o n b r a n c h . I t i s a v a i l a b l e s t a r t i n g f r o m p e r l 5 . 1 0 . 0 .
C a p t u r e g r o u p s a r e n u m b e r e d f r o m l e f t t o r i g h t , b u t i n s i d e t h i s c o n s t r u c t t h e n u m b e r i n g i s r e s t a r t e d f o r e a c h b r a n c h .
T h e n u m b e r i n g w i t h i n e a c h b r a n c h w i l l b e a s n o r m a l , a n d a n y g r o u p s f o l l o w i n g t h i s c o n s t r u c t w i l l b e n u m b e r e d a s t h o u g h t h e c o n s t r u c t c o n t a i n e d o n l y o n e b r a n c h , t h a t b e i n g t h e o n e w i t h t h e m o s t c a p t u r e g r o u p s i n i t .
T h i s c o n s t r u c t i s u s e f u l w h e n y o u w a n t t o c a p t u r e o n e o f a n u m b e r o f a l t e r n a t i v e m a t c h e s .
C o n s i d e r t h e f o l l o w i n g p a t t e r n . T h e n u m b e r s u n d e r n e a t h s h o w i n w h i c h g r o u p t h e c a p t u r e d c o n t e n t w i l l b e s t o r e d .
# before ---------------branch-reset----------- after
/ (a ) (?| x (y ) z | (p (q ) r) | (t ) u (v ) ) (z ) /x
# 1 2 2 3 2 3 4
B e c a r e f u l w h e n u s i n g t h e b r a n c h r e s e t p a t t e r n i n c o m b i n a t i o n w i t h n a m e d c a p t u r e s . N a m e d c a p t u r e s a r e i m p l e m e n t e d a s b e i n g a l i a s e s t o n u m b e r e d g r o u p s h o l d i n g t h e c a p t u r e s , a n d t h a t i n t e r f e r e s w i t h t h e i m p l e m e n t a t i o n o f t h e b r a n c h r e s e t p a t t e r n . I f y o u a r e u s i n g n a m e d c a p t u r e s i n a b r a n c h r e s e t p a t t e r n , i t ' s b e s t t o u s e t h e s a m e n a m e s , i n t h e s a m e o r d e r , i n e a c h o f t h e a l t e r n a t i o n s :
/(?| (?<a>x ) (?<b>y )
| (?<a>z ) (?<b>w )) /x
N o t d o i n g s o m a y l e a d t o s u r p r i s e s :
"12" =~ /(?| (?<a> \d+ ) | (?<b> \D+))/x;
say $+ {a}; # Prints '12'
say $+ {b}; # *Also* prints '12'.
T h e p r o b l e m h e r e i s t h a t b o t h t h e g r o u p n a m e d a
a n d t h e g r o u p n a m e d b
a r e a l i a s e s f o r t h e g r o u p b e l o n g i n g t o $ 1
.
# L o o k - A r o u n d A s s e r t i o n s
L o o k - a r o u n d a s s e r t i o n s a r e z e r o - w i d t h p a t t e r n s w h i c h m a t c h a s p e c i f i c p a t t e r n w i t h o u t i n c l u d i n g i t i n $ &
. P o s i t i v e a s s e r t i o n s m a t c h w h e n t h e i r s u b p a t t e r n m a t c h e s , n e g a t i v e a s s e r t i o n s m a t c h w h e n t h e i r s u b p a t t e r n f a i l s . L o o k - b e h i n d m a t c h e s t e x t u p t o t h e c u r r e n t m a t c h p o s i t i o n , l o o k - a h e a d m a t c h e s t e x t f o l l o w i n g t h e c u r r e n t m a t c h p o s i t i o n .
# ( ? = p a t t e r n )
A z e r o - w i d t h p o s i t i v e l o o k - a h e a d a s s e r t i o n . F o r e x a m p l e , /
\ w + ( ? = \ t ) /
m a t c h e s a w o r d f o l l o w e d b y a t a b , w i t h o u t i n c l u d i n g t h e t a b i n $ &
.
# ( ?! p a t t e r n )
A z e r o - w i d t h n e g a t i v e l o o k - a h e a d a s s e r t i o n . F o r e x a m p l e / f
o o ( ?! b a r ) /
m a t c h e s a n y o c c u r r e n c e o f " f o o " t h a t i s n ' t f o l l o w e d b y " b a r " . N o t e h o w e v e r t h a t l o o k - a h e a d a n d l o o k - b e h i n d a r e N O T t h e s a m e t h i n g . Y o u c a n n o t u s e t h i s f o r l o o k - b e h i n d .
I f y o u a r e l o o k i n g f o r a " b a r " t h a t i s n ' t p r e c e d e d b y a " f o o " , / ( ?! f o o ) b a r /
w i l l n o t d o w h a t y o u w a n t . T h a t ' s b e c a u s e t h e ( ?! f o o )
i s j u s t s a y i n g t h a t t h e n e x t t h i n g c a n n o t b e " f o o " - - a n d i t ' s n o t , i t ' s a " b a r " , s o " f o o b a r " w i l l m a t c h . U s e l o o k - b e h i n d i n s t e a d ( s e e b e l o w ) .
# ( ? < = p a t t e r n )
\ K
A z e r o - w i d t h p o s i t i v e l o o k - b e h i n d a s s e r t i o n . F o r e x a m p l e , / ( ? < = \ t ) \ w + /
m a t c h e s a w o r d t h a t f o l l o w s a t a b , w i t h o u t i n c l u d i n g t h e t a b i n $ &
. W o r k s o n l y f o r f i x e d - w i d t h l o o k - b e h i n d .
T h e r e i s a s p e c i a l f o r m o f t h i s c o n s t r u c t , c a l l e d \ K
, w h i c h c a u s e s t h e r e g e x e n g i n e t o " k e e p " e v e r y t h i n g i t h a d m a t c h e d p r i o r t o t h e \ K
a n d n o t i n c l u d e i t i n $ &
. T h i s e f f e c t i v e l y p r o v i d e s v a r i a b l e - l e n g t h l o o k - b e h i n d . T h e u s e o f \ K
i n s i d e o f a n o t h e r l o o k - a r o u n d a s s e r t i o n i s a l l o w e d , b u t t h e b e h a v i o u r i s c u r r e n t l y n o t w e l l d e f i n e d .
F o r v a r i o u s r e a s o n s \ K
m a y b e s i g n i f i c a n t l y m o r e e f f i c i e n t t h a n t h e e q u i v a l e n t ( ? < = . . . )
c o n s t r u c t , a n d i t i s e s p e c i a l l y u s e f u l i n s i t u a t i o n s w h e r e y o u w a n t t o e f f i c i e n t l y r e m o v e s o m e t h i n g f o l l o w i n g s o m e t h i n g e l s e i n a s t r i n g . F o r i n s t a n c e
s/(foo)bar/$1/g;
c a n b e r e w r i t t e n a s t h e m u c h m o r e e f f i c i e n t
s/foo\Kbar//g;
# ( ? < ! p a t t e r n )
A z e r o - w i d t h n e g a t i v e l o o k - b e h i n d a s s e r t i o n . F o r e x a m p l e /
( ? < ! b a r ) f o o /
m a t c h e s a n y o c c u r r e n c e o f " f o o " t h a t d o e s n o t f o l l o w " b a r " . W o r k s o n l y f o r f i x e d - w i d t h l o o k - b e h i n d .
# ( ? ' N A M E ' p a t t e r n )
# ( ? < N A M E > p a t t e r n )
A n a m e d c a p t u r e g r o u p . I d e n t i c a l i n e v e r y r e s p e c t t o n o r m a l c a p t u r i n g p a r e n t h e s e s ( )
b u t f o r t h e a d d i t i o n a l f a c t t h a t t h e g r o u p c a n b e r e f e r r e d t o b y n a m e i n v a r i o u s r e g u l a r e x p r e s s i o n c o n s t r u c t s ( l i k e \ g { N A M E }
) a n d c a n b e a c c e s s e d b y n a m e a f t e r a s u c c e s s f u l m a t c h v i a % +
or % -
. S e e p e r l v a r f o r m o r e d e t a i l s o n t h e % +
a n d % -
h a s h e s .
I f m u l t i p l e d i s t i n c t c a p t u r e g r o u p s h a v e t h e s a m e n a m e t h e n t h e $ + { N A M E } w i l l r e f e r t o t h e l e f t m o s t d e f i n e d g r o u p i n t h e m a t c h .
T h e f o r m s ( ? ' N A M E ' p a t t e r n )
a n d ( ? < N A M E > p a t t e r n )
a r e e q u i v a l e n t .
N O T E : W h i l e t h e n o t a t i o n o f t h i s c o n s t r u c t i s t h e s a m e a s t h e s i m i l a r f u n c t i o n i n . N E T r e g e x e s , t h e b e h a v i o r i s n o t . I n P e r l t h e g r o u p s a r e n u m b e r e d s e q u e n t i a l l y r e g a r d l e s s o f b e i n g n a m e d o r n o t . T h u s i n t h e p a t t e r n
/(x )(?<foo>y )(z )/
$ + { f o o } w i l l b e t h e s a m e a s $ 2 , a n d $ 3 w i l l c o n t a i n ' z ' i n s t e a d o f t h e o p p o s i t e w h i c h i s w h a t a . N E T r e g e x h a c k e r m i g h t e x p e c t .
C u r r e n t l y N A M E i s r e s t r i c t e d t o s i m p l e i d e n t i f i e r s o n l y . I n o t h e r w o r d s , i t m u s t m a t c h / ^ [ _ A -
Z a - z ] [ _ A - Z a - z 0 - 9 ] * \ z /
o r i t s U n i c o d e e x t e n s i o n ( s e e u t f 8 ) , t h o u g h i t i s n ' t e x t e n d e d b y t h e l o c a l e ( s e e p e r l l o c a l e ) .
N O T E : I n o r d e r t o m a k e t h i n g s e a s i e r f o r p r o g r a m m e r s w i t h e x p e r i e n c e w i t h t h e P y t h o n o r P C R E r e g e x e n g i n e s , t h e p a t t e r n ( ? P <
N A M E > p a t t e r n )
m a y b e u s e d i n s t e a d o f ( ? < N A M E > p a t t e r n )
; h o w e v e r t h i s f o r m d o e s n o t s u p p o r t t h e u s e o f s i n g l e q u o t e s a s a d e l i m i t e r f o r t h e n a m e .
# \ k < N A M E >
# \ k ' N A M E '
N a m e d b a c k r e f e r e n c e . S i m i l a r t o n u m e r i c b a c k r e f e r e n c e s , e x c e p t t h a t t h e g r o u p i s d e s i g n a t e d b y n a m e a n d n o t n u m b e r . I f m u l t i p l e g r o u p s h a v e t h e s a m e n a m e t h e n i t r e f e r s t o t h e l e f t m o s t d e f i n e d g r o u p i n t h e c u r r e n t m a t c h .
I t i s a n e r r o r t o r e f e r t o a n a m e n o t d e f i n e d b y a ( ? < N A M E > )
e a r l i e r i n t h e p a t t e r n .
B o t h f o r m s a r e e q u i v a l e n t .
N O T E : I n o r d e r t o m a k e t h i n g s e a s i e r f o r p r o g r a m m e r s w i t h e x p e r i e n c e w i t h t h e P y t h o n o r P C R E r e g e x e n g i n e s , t h e p a t t e r n ( ? P =
N A M E )
m a y b e u s e d i n s t e a d o f \ k
< N A M E >
.
# ( ? { c o d e } )
W A R N I N G : T h i s e x t e n d e d r e g u l a r e x p r e s s i o n f e a t u r e i s c o n s i d e r e d e x p e r i m e n t a l , a n d m a y b e c h a n g e d w i t h o u t n o t i c e . C o d e e x e c u t e d t h a t h a s s i d e e f f e c t s m a y n o t p e r f o r m i d e n t i c a l l y f r o m v e r s i o n t o v e r s i o n d u e t o t h e e f f e c t o f f u t u r e o p t i m i s a t i o n s i n t h e r e g e x e n g i n e .
T h i s z e r o - w i d t h a s s e r t i o n e v a l u a t e s a n y e m b e d d e d P e r l c o d e . I t a l w a y s s u c c e e d s , a n d i t s c o d e
i s n o t i n t e r p o l a t e d . C u r r e n t l y , t h e r u l e s t o d e t e r m i n e w h e r e t h e c o d e
e n d s a r e s o m e w h a t c o n v o l u t e d .
T h i s f e a t u r e c a n b e u s e d t o g e t h e r w i t h t h e s p e c i a l v a r i a b l e $ ^
N
t o c a p t u r e t h e r e s u l t s o f s u b m a t c h e s i n v a r i a b l e s w i t h o u t h a v i n g t o k e e p t r a c k o f t h e n u m b e r o f n e s t e d p a r e n t h e s e s . F o r e x a m p l e :
$_ = "The brown fox jumps over the lazy dog";
/the (\S+)(?{ $color = $^N }) (\S+)(?{ $animal = $^N })/i;
print "color = $color, animal = $animal\n";
I n s i d e t h e ( ? { . . . } )
b l o c k , $ _
r e f e r s t o t h e s t r i n g t h e r e g u l a r e x p r e s s i o n i s m a t c h i n g a g a i n s t . Y o u c a n a l s o u s e p o s ( )
t o k n o w w h a t i s t h e c u r r e n t p o s i t i o n o f m a t c h i n g w i t h i n t h i s s t r i n g .
T h e c o d e
i s p r o p e r l y s c o p e d i n t h e f o l l o w i n g s e n s e : I f t h e a s s e r t i o n i s b a c k t r a c k e d ( c o m p a r e " B a c k t r a c k i n g " ) , a l l c h a n g e s i n t r o d u c e d a f t e r l o c a l
i z a t i o n a r e u n d o n e , s o t h a t
$_ = 'a' x 8;
m<
(?{ $cnt = 0 }) # Initialize $cnt.
(
a
(?{
local $cnt = $cnt + 1; # Update $cnt,
# backtracking-safe.
})
)*
aaaa
(?{ $res = $cnt }) # On success copy to
# non-localized location.
>x;
w i l l s e t $ r e s = 4
. N o t e t h a t a f t e r t h e m a t c h , $ c n t
r e t u r n s t o t h e g l o b a l l y i n t r o d u c e d v a l u e , b e c a u s e t h e s c o p e s t h a t r e s t r i c t l o c a l
o p e r a t o r s a r e u n w o u n d .
T h i s a s s e r t i o n m a y b e u s e d a s a ( ? ( c o n d i t i o n ) y e s - p a t t e r n | n o - p a
t t e r n )
s w i t c h . I f n o t u s e d i n t h i s w a y , t h e r e s u l t o f e v a l u a t i o n o f c o d e
i s p u t i n t o t h e s p e c i a l v a r i a b l e $ ^ R
. T h i s h a p p e n s i m m e d i a t e l y , s o $ ^ R
c a n b e u s e d f r o m o t h e r ( ? { c o d e } )
a s s e r t i o n s i n s i d e t h e s a m e r e g u l a r e x p r e s s i o n .
T h e a s s i g n m e n t t o $ ^ R
a b o v e i s p r o p e r l y l o c a l i z e d , s o t h e o l d v a l u e o f $ ^ R
i s r e s t o r e d i f t h e a s s e r t i o n i s b a c k t r a c k e d ; c o m p a r e " B a c k t r a c k i n g " .
F o r r e a s o n s o f s e c u r i t y , t h i s c o n s t r u c t i s f o r b i d d e n i f t h e r e g u l a r e x p r e s s i o n i n v o l v e s r u n - t i m e i n t e r p o l a t i o n o f v a r i a b l e s , u n l e s s t h e p e r i l o u s u s e r e ' e v
a l '
p r a g m a h a s b e e n u s e d ( s e e re ) , o r t h e v a r i a b l e s c o n t a i n r e s u l t s o f t h e q r / /
o p e r a t o r ( s e e " q r / S T R I N G / m s i x p o d u a l " i n p e r l o p ) .
T h i s r e s t r i c t i o n i s d u e t o t h e w i d e - s p r e a d a n d r e m a r k a b l y c o n v e n i e n t c u s t o m o f u s i n g r u n - t i m e d e t e r m i n e d s t r i n g s a s p a t t e r n s . F o r e x a m p l e :
$re = <>;
chomp $re;
$string =~ /$re/;
B e f o r e P e r l k n e w h o w t o e x e c u t e i n t e r p o l a t e d c o d e w i t h i n a p a t t e r n , t h i s o p e r a t i o n w a s c o m p l e t e l y s a f e f r o m a s e c u r i t y p o i n t o f v i e w , a l t h o u g h i t c o u l d r a i s e a n e x c e p t i o n f r o m a n i l l e g a l p a t t e r n . I f y o u t u r n o n t h e u s
e r e ' e v a l '
, t h o u g h , i t i s n o l o n g e r s e c u r e , s o y o u s h o u l d o n l y d o s o i f y o u a r e a l s o u s i n g t a i n t c h e c k i n g . B e t t e r y e t , u s e t h e c a r e f u l l y c o n s t r a i n e d e v a l u a t i o n w i t h i n a S a f e c o m p a r t m e n t . S e e p e r l s e c f o r d e t a i l s a b o u t b o t h t h e s e m e c h a n i s m s .
W A R N I N G : U s e o f l e x i c a l ( my
) v a r i a b l e s i n t h e s e b l o c k s i s b r o k e n . T h e r e s u l t i s u n p r e d i c t a b l e a n d w i l l m a k e p e r l u n s t a b l e . T h e w o r k a r o u n d i s t o u s e g l o b a l ( o u r
) v a r i a b l e s .
W A R N I N G : I n p e r l 5 . 1 2 . x a n d e a r l i e r , t h e r e g e x e n g i n e w a s n o t r e - e n t r a n t , s o i n t e r p o l a t e d c o d e c o u l d n o t s a f e l y i n v o k e t h e r e g e x e n g i n e e i t h e r d i r e c t l y w i t h m / /
or s / / /
) , o r i n d i r e c t l y w i t h f u n c t i o n s s u c h a s s p l i t
. I n v o k i n g t h e r e g e x e n g i n e i n t h e s e b l o c k s w o u l d m a k e p e r l u n s t a b l e .
# ( ?? { c o d e } )
W A R N I N G : T h i s e x t e n d e d r e g u l a r e x p r e s s i o n f e a t u r e i s c o n s i d e r e d e x p e r i m e n t a l , a n d m a y b e c h a n g e d w i t h o u t n o t i c e . C o d e e x e c u t e d t h a t h a s s i d e e f f e c t s m a y n o t p e r f o r m i d e n t i c a l l y f r o m v e r s i o n t o v e r s i o n d u e t o t h e e f f e c t o f f u t u r e o p t i m i s a t i o n s i n t h e r e g e x e n g i n e .
T h i s i s a " p o s t p o n e d " r e g u l a r s u b e x p r e s s i o n . T h e c o d e
i s e v a l u a t e d a t r u n t i m e , a t t h e m o m e n t t h i s s u b e x p r e s s i o n m a y m a t c h . T h e r e s u l t o f e v a l u a t i o n i s c o n s i d e r e d a r e g u l a r e x p r e s s i o n a n d m a t c h e d a s i f i t w e r e i n s e r t e d i n s t e a d o f t h i s c o n s t r u c t . N o t e t h a t t h i s m e a n s t h a t t h e c o n t e n t s o f c a p t u r e g r o u p s d e f i n e d i n s i d e a n e v a l ' e d p a t t e r n a r e n o t a v a i l a b l e o u t s i d e o f t h e p a t t e r n , a n d v i c e v e r s a , t h e r e i s n o w a y f o r t h e i n n e r p a t t e r n r e t u r n e d f r o m t h e c o d e b l o c k t o r e f e r t o a c a p t u r e g r o u p d e f i n e d o u t s i d e . ( T h e c o d e b l o c k i t s e l f c a n u s e $ 1
, e t c . , t o r e f e r t o t h e e n c l o s i n g p a t t e r n ' s c a p t u r e g r o u p s . ) T h u s ,
('a' x 100)=~/(??{'(.)' x 100})/
w i l l m a t c h , i t w i l l n o t s e t $ 1 .
T h e c o d e
i s n o t i n t e r p o l a t e d . A s b e f o r e , t h e r u l e s t o d e t e r m i n e w h e r e t h e c o d e
e n d s a r e c u r r e n t l y s o m e w h a t c o n v o l u t e d .
T h e f o l l o w i n g p a t t e r n m a t c h e s a p a r e n t h e s i z e d g r o u p :
$re = qr{
\(
(?:
(?> [^()]+ ) # Non-parens without backtracking
|
(??{ $re }) # Group with matching parens
)*
\)
}x;
S e e a l s o ( ? P A R N O )
f o r a d i f f e r e n t , m o r e e f f i c i e n t w a y t o a c c o m p l i s h t h e s a m e t a s k .
F o r r e a s o n s o f s e c u r i t y , t h i s c o n s t r u c t i s f o r b i d d e n i f t h e r e g u l a r e x p r e s s i o n i n v o l v e s r u n - t i m e i n t e r p o l a t i o n o f v a r i a b l e s , u n l e s s t h e p e r i l o u s u s e r e ' e v
a l '
p r a g m a h a s b e e n u s e d ( s e e re ) , o r t h e v a r i a b l e s c o n t a i n r e s u l t s o f t h e q r / /
o p e r a t o r ( s e e " q r / S T R I N G / m s i x p o d u a l " i n p e r l o p ) .
I n p e r l 5 . 1 2 . x a n d e a r l i e r , b e c a u s e t h e r e g e x e n g i n e w a s n o t r e - e n t r a n t , d e l a y e d c o d e c o u l d n o t s a f e l y i n v o k e t h e r e g e x e n g i n e e i t h e r d i r e c t l y w i t h m / /
or s / /
/
) , o r i n d i r e c t l y w i t h f u n c t i o n s s u c h a s s p l i t
.
R e c u r s i n g d e e p e r t h a n 5 0 t i m e s w i t h o u t c o n s u m i n g a n y i n p u t s t r i n g w i l l r e s u l t i n a f a t a l e r r o r . T h e m a x i m u m d e p t h i s c o m p i l e d i n t o p e r l , s o c h a n g i n g i t r e q u i r e s a c u s t o m b u i l d .
# ( ? P A R N O )
( ? - P A R N O )
( ? + P A R N O )
( ?
R )
( ? 0 )
S i m i l a r t o ( ?? { c o d e } )
e x c e p t i t d o e s n o t i n v o l v e c o m p i l i n g a n y c o d e , i n s t e a d i t t r e a t s t h e c o n t e n t s o f a c a p t u r e g r o u p a s a n i n d e p e n d e n t p a t t e r n t h a t m u s t m a t c h a t t h e c u r r e n t p o s i t i o n . C a p t u r e g r o u p s c o n t a i n e d b y t h e p a t t e r n w i l l h a v e t h e v a l u e a s d e t e r m i n e d b y t h e o u t e r m o s t r e c u r s i o n .
P A R N O i s a s e q u e n c e o f d i g i t s ( n o t s t a r t i n g w i t h 0 ) w h o s e v a l u e r e f l e c t s t h e p a r e n - n u m b e r o f t h e c a p t u r e g r o u p t o r e c u r s e t o . ( ? R )
r e c u r s e s t o t h e b e g i n n i n g o f t h e w h o l e p a t t e r n . ( ? 0 )
i s a n a l t e r n a t e s y n t a x f o r ( ? R )
. I f P A R N O i s p r e c e d e d b y a p l u s o r m i n u s s i g n t h e n i t i s a s s u m e d t o b e r e l a t i v e , w i t h n e g a t i v e n u m b e r s i n d i c a t i n g p r e c e d i n g c a p t u r e g r o u p s a n d p o s i t i v e o n e s f o l l o w i n g . T h u s ( ? - 1 )
r e f e r s t o t h e m o s t r e c e n t l y d e c l a r e d g r o u p , a n d ( ? + 1 )
i n d i c a t e s t h e n e x t g r o u p t o b e d e c l a r e d . N o t e t h a t t h e c o u n t i n g f o r r e l a t i v e r e c u r s i o n d i f f e r s f r o m t h a t o f r e l a t i v e b a c k r e f e r e n c e s , i n t h a t w i t h r e c u r s i o n u n c l o s e d g r o u p s a r e i n c l u d e d .
T h e f o l l o w i n g p a t t e r n m a t c h e s a f u n c t i o n f o o ( ) w h i c h m a y c o n t a i n b a l a n c e d p a r e n t h e s e s a s t h e a r g u m e n t .
$re = qr{ ( # paren group 1 (full function)
foo
( # paren group 2 (parens)
\(
( # paren group 3 (contents of parens)
(?:
(?> [^()]+ ) # Non-parens without backtracking
|
(?2) # Recurse to start of paren group 2
)*
)
\)
)
)
}x;
I f t h e p a t t e r n w a s u s e d a s f o l l o w s
'foo(bar(baz)+baz(bop))'=~/$re/
and print "\$1 = $1\n",
"\$2 = $2\n",
"\$3 = $3\n";
t h e o u t p u t p r o d u c e d s h o u l d b e t h e f o l l o w i n g :
$1 = foo(bar(baz)+baz(bop))
$2 = (bar(baz)+baz(bop))
$3 = bar(baz)+baz(bop)
I f t h e r e i s n o c o r r e s p o n d i n g c a p t u r e g r o u p d e f i n e d , t h e n i t i s a f a t a l e r r o r . R e c u r s i n g d e e p e r t h a n 5 0 t i m e s w i t h o u t c o n s u m i n g a n y i n p u t s t r i n g w i l l a l s o r e s u l t i n a f a t a l e r r o r . T h e m a x i m u m d e p t h i s c o m p i l e d i n t o p e r l , s o c h a n g i n g i t r e q u i r e s a c u s t o m b u i l d .
T h e f o l l o w i n g s h o w s h o w u s i n g n e g a t i v e i n d e x i n g c a n m a k e i t e a s i e r t o e m b e d r e c u r s i v e p a t t e r n s i n s i d e o f a q r / /
c o n s t r u c t f o r l a t e r u s e :
my $parens = qr/(\((?:[^()]++|(?-1))*+\))/;
if (/foo $parens \s+ + \s+ bar $parens/x) {
# do something here...
}
N o t e t h a t t h i s p a t t e r n d o e s n o t b e h a v e t h e s a m e w a y a s t h e e q u i v a l e n t P C R E o r P y t h o n c o n s t r u c t o f t h e s a m e f o r m . I n P e r l y o u c a n b a c k t r a c k i n t o a r e c u r s e d g r o u p , i n P C R E a n d P y t h o n t h e r e c u r s e d i n t o g r o u p i s t r e a t e d a s a t o m i c . A l s o , m o d i f i e r s a r e r e s o l v e d a t c o m p i l e t i m e , s o c o n s t r u c t s l i k e ( ? i : ( ? 1 ) ) o r ( ? : ( ? i ) ( ? 1 ) ) d o n o t a f f e c t h o w t h e s u b - p a t t e r n w i l l b e p r o c e s s e d .
# ( ? & N A M E )
R e c u r s e t o a n a m e d s u b p a t t e r n . I d e n t i c a l t o ( ? P A R N O )
e x c e p t t h a t t h e p a r e n t h e s i s t o r e c u r s e t o i s d e t e r m i n e d b y n a m e . I f m u l t i p l e p a r e n t h e s e s h a v e t h e s a m e n a m e , t h e n i t r e c u r s e s t o t h e l e f t m o s t .
I t i s a n e r r o r t o r e f e r t o a n a m e t h a t i s n o t d e c l a r e d s o m e w h e r e i n t h e p a t t e r n .
N O T E : I n o r d e r t o m a k e t h i n g s e a s i e r f o r p r o g r a m m e r s w i t h e x p e r i e n c e w i t h t h e P y t h o n o r P C R E r e g e x e n g i n e s t h e p a t t e r n ( ? P > N
A M E )
m a y b e u s e d i n s t e a d o f ( ? &
N A M E )
.
# ( ? ( c o n d i t i o n ) y e s - p a t t e r n | n o - p a t
t e r n )
# ( ? ( c o n d i t i o n ) y e s - p a t t e r n )
C o n d i t i o n a l e x p r e s s i o n . M a t c h e s y e s - p a t t e r n
if c o n d i t i o n
y i e l d s a t r u e v a l u e , m a t c h e s n o - p a t t e r n
o t h e r w i s e . A m i s s i n g p a t t e r n a l w a y s m a t c h e s .
( c o n d i t i o n )
s h o u l d b e e i t h e r a n i n t e g e r i n p a r e n t h e s e s ( w h i c h i s v a l i d i f t h e c o r r e s p o n d i n g p a i r o f p a r e n t h e s e s m a t c h e d ) , a l o o k - a h e a d / l o o k - b e h i n d / e v a l u a t e z e r o - w i d t h a s s e r t i o n , a n a m e i n a n g l e b r a c k e t s o r s i n g l e q u o t e s ( w h i c h i s v a l i d i f a g r o u p w i t h t h e g i v e n n a m e m a t c h e d ) , o r t h e s p e c i a l s y m b o l ( R ) ( t r u e w h e n e v a l u a t e d i n s i d e o f r e c u r s i o n o r e v a l ) . A d d i t i o n a l l y t h e R m a y b e f o l l o w e d b y a n u m b e r , ( w h i c h w i l l b e t r u e w h e n e v a l u a t e d w h e n r e c u r s i n g i n s i d e o f t h e a p p r o p r i a t e g r o u p ) , o r b y & N A M
E
, i n w h i c h c a s e i t w i l l b e t r u e o n l y w h e n e v a l u a t e d d u r i n g r e c u r s i o n i n t h e n a m e d g r o u p .
H e r e ' s a s u m m a r y o f t h e p o s s i b l e p r e d i c a t e s :
# ( 1 ) ( 2 ) . . .
C h e c k s i f t h e n u m b e r e d c a p t u r i n g g r o u p h a s m a t c h e d s o m e t h i n g .
# ( < N A M E > ) ( ' N A M E ' )
C h e c k s i f a g r o u p w i t h t h e g i v e n n a m e h a s m a t c h e d s o m e t h i n g .
# ( ? = . . . ) ( ?! . . . ) ( ? < = . . . ) ( ? < ! . . . )
C h e c k s w h e t h e r t h e p a t t e r n m a t c h e s ( o r d o e s n o t m a t c h , f o r t h e ' ! ' v a r i a n t s ) .
# ( ? { C O D E } )
T r e a t s t h e r e t u r n v a l u e o f t h e c o d e b l o c k a s t h e c o n d i t i o n .
# ( R )
C h e c k s i f t h e e x p r e s s i o n h a s b e e n e v a l u a t e d i n s i d e o f r e c u r s i o n .
# ( R 1 ) ( R 2 ) . . .
C h e c k s i f t h e e x p r e s s i o n h a s b e e n e v a l u a t e d w h i l e e x e c u t i n g d i r e c t l y i n s i d e o f t h e n - t h c a p t u r e g r o u p . T h i s c h e c k i s t h e r e g e x e q u i v a l e n t o f
if ((caller(0))[3 ] eq 'subname') { ... }
I n o t h e r w o r d s , i t d o e s n o t c h e c k t h e f u l l r e c u r s i o n s t a c k .
# ( R & N A M E )
S i m i l a r t o ( R 1 )
, t h i s p r e d i c a t e c h e c k s t o s e e i f w e ' r e e x e c u t i n g d i r e c t l y i n s i d e o f t h e l e f t m o s t g r o u p w i t h a g i v e n n a m e ( t h i s i s t h e s a m e l o g i c u s e d b y ( ? & N A M E )
t o d i s a m b i g u a t e ) . I t d o e s n o t c h e c k t h e f u l l s t a c k , b u t o n l y t h e n a m e o f t h e i n n e r m o s t a c t i v e r e c u r s i o n .
# ( D E F I N E )
I n t h i s c a s e , t h e y e s - p a t t e r n i s n e v e r d i r e c t l y e x e c u t e d , a n d n o n o - p a t t e r n i s a l l o w e d . S i m i l a r i n s p i r i t t o ( ? { 0 } )
b u t m o r e e f f i c i e n t . S e e b e l o w f o r d e t a i l s .
F o r e x a m p l e :
m{ ( \( )?
[^()]+
(?(1 ) \) )
}x
m a t c h e s a c h u n k o f n o n - p a r e n t h e s e s , p o s s i b l y i n c l u d e d i n p a r e n t h e s e s t h e m s e l v e s .
A s p e c i a l f o r m i s t h e ( D E F I N E )
p r e d i c a t e , w h i c h n e v e r e x e c u t e s i t s y e s - p a t t e r n d i r e c t l y , a n d d o e s n o t a l l o w a n o - p a t t e r n . T h i s a l l o w s o n e t o d e f i n e s u b p a t t e r n s w h i c h w i l l b e e x e c u t e d o n l y b y t h e r e c u r s i o n m e c h a n i s m . T h i s w a y , y o u c a n d e f i n e a s e t o f r e g u l a r e x p r e s s i o n r u l e s t h a t c a n b e b u n d l e d i n t o a n y p a t t e r n y o u c h o o s e .
I t i s r e c o m m e n d e d t h a t f o r t h i s u s a g e y o u p u t t h e D E F I N E b l o c k a t t h e e n d o f t h e p a t t e r n , a n d t h a t y o u n a m e a n y s u b p a t t e r n s d e f i n e d w i t h i n i t .
A l s o , i t ' s w o r t h n o t i n g t h a t p a t t e r n s d e f i n e d t h i s w a y p r o b a b l y w i l l n o t b e a s e f f i c i e n t , a s t h e o p t i m i s e r i s n o t v e r y c l e v e r a b o u t h a n d l i n g t h e m .
A n e x a m p l e o f h o w t h i s m i g h t b e u s e d i s a s f o l l o w s :
/(?<NAME>(?&NAME_PAT))(?<ADDR>(?&ADDRESS_PAT))
(?(DEFINE)
(?<NAME_PAT>....)
(?<ADRESS_PAT>....)
)/x
N o t e t h a t c a p t u r e g r o u p s m a t c h e d i n s i d e o f r e c u r s i o n a r e n o t a c c e s s i b l e a f t e r t h e r e c u r s i o n r e t u r n s , s o t h e e x t r a l a y e r o f c a p t u r i n g g r o u p s i s n e c e s s a r y . T h u s $ + { N A M E _ P A T }
w o u l d n o t b e d e f i n e d e v e n t h o u g h $ + { N A M E }
w o u l d b e .
F i n a l l y , k e e p i n m i n d t h a t s u b p a t t e r n s c r e a t e d i n s i d e a D E F I N E b l o c k c o u n t t o w a r d s t h e a b s o l u t e a n d r e l a t i v e n u m b e r o f c a p t u r e s , s o t h i s :
my @captures = "a" =~ /(.) # First capture
(?(DEFINE)
(?<EXAMPLE>1 ) # Second capture
)/x;
say scalar @captures;
W i l l o u t p u t 2 , n o t 1 . T h i s i s p a r t i c u l a r l y i m p o r t a n t i f y o u i n t e n d t o c o m p i l e t h e d e f i n i t i o n s w i t h t h e q r / /
o p e r a t o r , a n d l a t e r i n t e r p o l a t e t h e m i n a n o t h e r p a t t e r n .
# ( ? > p a t t e r n )
A n " i n d e p e n d e n t " s u b e x p r e s s i o n , o n e w h i c h m a t c h e s t h e s u b s t r i n g t h a t a s t a n d a l o n e p a t t e r n
w o u l d m a t c h i f a n c h o r e d a t t h e g i v e n p o s i t i o n , a n d i t m a t c h e s n o t h i n g o t h e r t h a n t h i s s u b s t r i n g . T h i s c o n s t r u c t i s u s e f u l f o r o p t i m i z a t i o n s o f w h a t w o u l d o t h e r w i s e b e " e t e r n a l " m a t c h e s , b e c a u s e i t w i l l n o t b a c k t r a c k ( s e e " B a c k t r a c k i n g " ) . I t m a y a l s o b e u s e f u l i n p l a c e s w h e r e t h e " g r a b a l l y o u c a n , a n d d o n o t g i v e a n y t h i n g b a c k " s e m a n t i c i s d e s i r a b l e .
F o r e x a m p l e : ^ ( ? > a * ) a b
w i l l n e v e r m a t c h , s i n c e ( ? > a * )
( a n c h o r e d a t t h e b e g i n n i n g o f s t r i n g , a s a b o v e ) w i l l m a t c h a l l c h a r a c t e r s a
a t t h e b e g i n n i n g o f s t r i n g , l e a v i n g n o a
f o r ab
t o m a t c h . I n c o n t r a s t , a * a b
w i l l m a t c h t h e s a m e a s a + b
, s i n c e t h e m a t c h o f t h e s u b g r o u p a *
i s i n f l u e n c e d b y t h e f o l l o w i n g g r o u p ab
( s e e " B a c k t r a c k i n g " ) . I n p a r t i c u l a r , a *
i n s i d e a * a b
w i l l m a t c h f e w e r c h a r a c t e r s t h a n a s t a n d a l o n e a *
, s i n c e t h i s m a k e s t h e t a i l m a t c h .
( ? > p a t t e r n )
d o e s n o t d i s a b l e b a c k t r a c k i n g a l t o g e t h e r o n c e i t h a s m a t c h e d . I t i s s t i l l p o s s i b l e t o b a c k t r a c k p a s t t h e c o n s t r u c t , b u t n o t i n t o i t . S o ( ( ? > a * )
| ( ? > b * ) ) a r
w i l l s t i l l m a t c h " b a r " .
A n e f f e c t s i m i l a r t o ( ? > p a t t e r n )
m a y b e a c h i e v e d b y w r i t i n g ( ? =
( p a t t e r n ) ) \ g { - 1 }
. T h i s m a t c h e s t h e s a m e s u b s t r i n g a s a s t a n d a l o n e a +
, a n d t h e f o l l o w i n g \ g { - 1 }
e a t s t h e m a t c h e d s t r i n g ; i t t h e r e f o r e m a k e s a z e r o - l e n g t h a s s e r t i o n i n t o a n a n a l o g u e o f ( ? > . .
. )
. ( T h e d i f f e r e n c e b e t w e e n t h e s e t w o c o n s t r u c t s i s t h a t t h e s e c o n d o n e u s e s a c a p t u r i n g g r o u p , t h u s s h i f t i n g o r d i n a l s o f b a c k r e f e r e n c e s i n t h e r e s t o f a r e g u l a r e x p r e s s i o n . )
C o n s i d e r t h i s p a t t e r n :
m{ \(
(
[^()]+ # x+
|
\( [^()]* \)
)+
\)
}x
T h a t w i l l e f f i c i e n t l y m a t c h a n o n e m p t y g r o u p w i t h m a t c h i n g p a r e n t h e s e s t w o l e v e l s d e e p o r l e s s . H o w e v e r , i f t h e r e i s n o s u c h g r o u p , i t w i l l t a k e v i r t u a l l y f o r e v e r o n a l o n g s t r i n g . T h a t ' s b e c a u s e t h e r e a r e s o m a n y d i f f e r e n t w a y s t o s p l i t a l o n g s t r i n g i n t o s e v e r a l s u b s t r i n g s . T h i s i s w h a t ( . + ) +
i s d o i n g , a n d ( . + ) +
i s s i m i l a r t o a s u b p a t t e r n o f t h e a b o v e p a t t e r n . C o n s i d e r h o w t h e p a t t e r n a b o v e d e t e c t s n o - m a t c h o n ( ( ( ) a a a a a a a a a a a a a a
a a a a
i n s e v e r a l s e c o n d s , b u t t h a t e a c h e x t r a l e t t e r d o u b l e s t h i s t i m e . T h i s e x p o n e n t i a l p e r f o r m a n c e w i l l m a k e i t a p p e a r t h a t y o u r p r o g r a m h a s h u n g . H o w e v e r , a t i n y c h a n g e t o t h i s p a t t e r n
m{ \(
(
(?> [^()]+ ) # change x+ above to (?> x+ )
|
\( [^()]* \)
)+
\)
}x
w h i c h u s e s ( ? > . . . )
m a t c h e s e x a c t l y w h e n t h e o n e a b o v e d o e s ( v e r i f y i n g t h i s y o u r s e l f w o u l d b e a p r o d u c t i v e e x e r c i s e ) , b u t f i n i s h e s i n a f o u r t h t h e t i m e w h e n u s e d o n a s i m i l a r s t r i n g w i t h 1 0 0 0 0 0 0 a
s . B e a w a r e , h o w e v e r , t h a t , w h e n t h i s c o n s t r u c t i s f o l l o w e d b y a q u a n t i f i e r , i t c u r r e n t l y t r i g g e r s a w a r n i n g m e s s a g e u n d e r t h e u s e w a r n i n g s
p r a g m a o r - w s w i t c h s a y i n g i t " m a t c h e
s n u l l s t r i n g m a n y t i m e s i n r e g
e x "
.
O n s i m p l e g r o u p s , s u c h a s t h e p a t t e r n ( ? > [ ^ ( ) ] + )
, a c o m p a r a b l e e f f e c t m a y b e a c h i e v e d b y n e g a t i v e l o o k - a h e a d , a s i n [ ^ ( ) ] +
( ?! [ ^ ( ) ] )
. T h i s w a s o n l y 4 t i m e s s l o w e r o n a s t r i n g w i t h 1 0 0 0 0 0 0 a
s .
T h e " g r a b a l l y o u c a n , a n d d o n o t g i v e a n y t h i n g b a c k " s e m a n t i c i s d e s i r a b l e i n m a n y s i t u a t i o n s w h e r e o n t h e f i r s t s i g h t a s i m p l e ( ) *
l o o k s l i k e t h e c o r r e c t s o l u t i o n . S u p p o s e w e p a r s e t e x t w i t h c o m m e n t s b e i n g d e l i m i t e d b y #
f o l l o w e d b y s o m e o p t i o n a l ( h o r i z o n t a l ) w h i t e s p a c e . C o n t r a r y t o i t s a p p e a r a n c e , # [ \ t ] *
i s n o t t h e c o r r e c t s u b e x p r e s s i o n t o m a t c h t h e c o m m e n t d e l i m i t e r , b e c a u s e i t m a y " g i v e u p " s o m e w h i t e s p a c e i f t h e r e m a i n d e r o f t h e p a t t e r n c a n b e m a d e t o m a t c h t h a t w a y . T h e c o r r e c t a n s w e r i s e i t h e r o n e o f t h e s e :
(?>#[ \t]*)
#[ \t]*(?![ \t])
F o r e x a m p l e , t o g r a b n o n - e m p t y c o m m e n t s i n t o $ 1 , o n e s h o u l d u s e e i t h e r o n e o f t h e s e :
/ (?> \# [ \t]* ) ( .+ ) /x;
/ \# [ \t]* ( [^ \t] .* ) /x;
W h i c h o n e y o u p i c k d e p e n d s o n w h i c h o f t h e s e e x p r e s s i o n s b e t t e r r e f l e c t s t h e a b o v e s p e c i f i c a t i o n o f c o m m e n t s .
I n s o m e l i t e r a t u r e t h i s c o n s t r u c t i s c a l l e d " a t o m i c m a t c h i n g " o r " p o s s e s s i v e m a t c h i n g " .
P o s s e s s i v e q u a n t i f i e r s a r e e q u i v a l e n t t o p u t t i n g t h e i t e m t h e y a r e a p p l i e d t o i n s i d e o f o n e o f t h e s e c o n s t r u c t s . T h e f o l l o w i n g e q u i v a l e n c e s a p p l y :
Quantifier Form Bracketing Form
--------------- ---------------
PAT*+ (?>PAT*)
PAT++ (?>PAT+)
PAT?+ (?>PAT?)
PAT{min,max}+ (?>PAT{min,max})
# S p e c i a l B a c k t r a c k i n g C o n t r o l V e r b s
W A R N I N G : T h e s e p a t t e r n s a r e e x p e r i m e n t a l a n d s u b j e c t t o c h a n g e o r r e m o v a l i n a f u t u r e v e r s i o n o f P e r l . T h e i r u s a g e i n p r o d u c t i o n c o d e s h o u l d b e n o t e d t o a v o i d p r o b l e m s d u r i n g u p g r a d e s .
T h e s e s p e c i a l p a t t e r n s a r e g e n e r a l l y o f t h e f o r m ( * V E R B : A R G )
. U n l e s s o t h e r w i s e s t a t e d t h e A R G a r g u m e n t i s o p t i o n a l ; i n s o m e c a s e s , i t i s f o r b i d d e n .
A n y p a t t e r n c o n t a i n i n g a s p e c i a l b a c k t r a c k i n g v e r b t h a t a l l o w s a n a r g u m e n t h a s t h e s p e c i a l b e h a v i o u r t h a t w h e n e x e c u t e d i t s e t s t h e c u r r e n t p a c k a g e ' s $ R E G E R R O R
a n d $ R E G M A R K
v a r i a b l e s . W h e n d o i n g s o t h e f o l l o w i n g r u l e s a p p l y :
O n f a i l u r e , t h e $ R E G E R R O R
v a r i a b l e w i l l b e s e t t o t h e A R G v a l u e o f t h e v e r b p a t t e r n , i f t h e v e r b w a s i n v o l v e d i n t h e f a i l u r e o f t h e m a t c h . I f t h e A R G p a r t o f t h e p a t t e r n w a s o m i t t e d , t h e n $ R E G
E R R O R
w i l l b e s e t t o t h e n a m e o f t h e l a s t ( * M A R K : N A M E )
p a t t e r n e x e c u t e d , o r t o T R U E i f t h e r e w a s n o n e . A l s o , t h e $ R E G M A R K
v a r i a b l e w i l l b e s e t t o F A L S E .
O n a s u c c e s s f u l m a t c h , t h e $ R E G E
R R O R
v a r i a b l e w i l l b e s e t t o F A L S E , a n d t h e $ R E G M A R K
v a r i a b l e w i l l b e s e t t o t h e n a m e o f t h e l a s t ( * M A R K : N A M E )
p a t t e r n e x e c u t e d . S e e t h e e x p l a n a t i o n f o r t h e ( * M
A R K : N A M E )
v e r b b e l o w f o r m o r e d e t a i l s .
N O T E : $ R E G E R R O R
a n d $ R E G M A R K
a r e n o t m a g i c v a r i a b l e s l i k e $ 1
a n d m o s t o t h e r r e g e x - r e l a t e d v a r i a b l e s . T h e y a r e n o t l o c a l t o a s c o p e , n o r r e a d o n l y , b u t i n s t e a d a r e v o l a t i l e p a c k a g e v a r i a b l e s s i m i l a r t o $ A U T O L O A D
. U s e l o c a l
t o l o c a l i z e c h a n g e s t o t h e m t o a s p e c i f i c s c o p e i f n e c e s s a r y .
I f a p a t t e r n d o e s n o t c o n t a i n a s p e c i a l b a c k t r a c k i n g v e r b t h a t a l l o w s a n a r g u m e n t , t h e n $ R E G E R R O
R
a n d $ R E G M A R K
a r e n o t t o u c h e d a t a l l .
# V e r b s t h a t t a k e a n a r g u m e n t
# ( * P R U N E )
( * P R U N E : N A M E )
T h i s z e r o - w i d t h p a t t e r n p r u n e s t h e b a c k t r a c k i n g t r e e a t t h e c u r r e n t p o i n t w h e n b a c k t r a c k e d i n t o o n f a i l u r e . C o n s i d e r t h e p a t t e r n A ( * P R U N E ) B
, w h e r e A a n d B a r e c o m p l e x p a t t e r n s . U n t i l t h e ( * P R U N E )
v e r b i s r e a c h e d , A m a y b a c k t r a c k a s n e c e s s a r y t o m a t c h . O n c e i t i s r e a c h e d , m a t c h i n g c o n t i n u e s i n B , w h i c h m a y a l s o b a c k t r a c k a s n e c e s s a r y ; h o w e v e r , s h o u l d B n o t m a t c h , t h e n n o f u r t h e r b a c k t r a c k i n g w i l l t a k e p l a c e , a n d t h e p a t t e r n w i l l f a i l o u t r i g h t a t t h e c u r r e n t s t a r t i n g p o s i t i o n .
T h e f o l l o w i n g e x a m p l e c o u n t s a l l t h e p o s s i b l e m a t c h i n g s t r i n g s i n a p a t t e r n ( w i t h o u t a c t u a l l y m a t c h i n g a n y o f t h e m ) .
'aaab' =~ /a+b?(?{print "$&\n"; $count++})(*FAIL)/;
print "Count=$count\n";
w h i c h p r o d u c e s :
aaab
aaa
aa
a
aab
aa
a
ab
a
Count=9
I f w e a d d a ( * P R U N E )
b e f o r e t h e c o u n t l i k e t h e f o l l o w i n g
'aaab' =~ /a+b?(*PRUNE)(?{print "$&\n"; $count++})(*FAIL)/;
print "Count=$count\n";
w e p r e v e n t b a c k t r a c k i n g a n d f i n d t h e c o u n t o f t h e l o n g e s t m a t c h i n g s t r i n g a t e a c h m a t c h i n g s t a r t i n g p o i n t l i k e s o :
aaab
aab
ab
Count=3
A n y n u m b e r o f ( * P R U N E )
a s s e r t i o n s m a y b e u s e d i n a p a t t e r n .
S e e a l s o ( ? > p a t t e r n )
a n d p o s s e s s i v e q u a n t i f i e r s f o r o t h e r w a y s t o c o n t r o l b a c k t r a c k i n g . I n s o m e c a s e s , t h e u s e o f ( *
P R U N E )
c a n b e r e p l a c e d w i t h a ( ? > p a t t e r n )
w i t h n o f u n c t i o n a l d i f f e r e n c e ; h o w e v e r , ( * P R U
N E )
c a n b e u s e d t o h a n d l e c a s e s t h a t c a n n o t b e e x p r e s s e d u s i n g a ( ? > p a t t e r n )
a l o n e .
# ( * S K I P )
( * S K I P : N A M E )
T h i s z e r o - w i d t h p a t t e r n i s s i m i l a r t o ( * P R U N E )
, e x c e p t t h a t o n f a i l u r e i t a l s o s i g n i f i e s t h a t w h a t e v e r t e x t t h a t w a s m a t c h e d l e a d i n g u p t o t h e ( *
S K I P )
p a t t e r n b e i n g e x e c u t e d c a n n o t b e p a r t o f a n y m a t c h o f t h i s p a t t e r n . T h i s e f f e c t i v e l y m e a n s t h a t t h e r e g e x e n g i n e " s k i p s " f o r w a r d t o t h i s p o s i t i o n o n f a i l u r e a n d t r i e s t o m a t c h a g a i n , ( a s s u m i n g t h a t t h e r e i s s u f f i c i e n t r o o m t o m a t c h ) .
T h e n a m e o f t h e ( * S K I P : N A M E )
p a t t e r n h a s s p e c i a l s i g n i f i c a n c e . I f a ( * M A R K : N A M E )
w a s e n c o u n t e r e d w h i l e m a t c h i n g , t h e n i t i s t h a t p o s i t i o n w h i c h i s u s e d a s t h e " s k i p p o i n t " . I f n o ( * M A R K )
o f t h a t n a m e w a s e n c o u n t e r e d , t h e n t h e ( * S K I P )
o p e r a t o r h a s n o e f f e c t . W h e n u s e d w i t h o u t a n a m e t h e " s k i p p o i n t " i s w h e r e t h e m a t c h p o i n t w a s w h e n e x e c u t i n g t h e ( * S K I P ) p a t t e r n .
C o m p a r e t h e f o l l o w i n g t o t h e e x a m p l e s i n ( * P R U N E )
; n o t e t h e s t r i n g i s t w i c e a s l o n g :
'aaabaaab' =~ /a+b?(*SKIP)(?{print "$&\n"; $count++})(*FAIL)/;
print "Count=$count\n";
o u t p u t s
aaab
aaab
Count=2
O n c e t h e ' a a a b ' a t t h e s t a r t o f t h e s t r i n g h a s m a t c h e d , a n d t h e ( * S K I P )
e x e c u t e d , t h e n e x t s t a r t i n g p o i n t w i l l b e w h e r e t h e c u r s o r w a s w h e n t h e (
* S K I P )
w a s e x e c u t e d .
# ( * M A R K : N A M E )
( * : N A M E )
T h i s z e r o - w i d t h p a t t e r n c a n b e u s e d t o m a r k t h e p o i n t r e a c h e d i n a s t r i n g w h e n a c e r t a i n p a r t o f t h e p a t t e r n h a s b e e n s u c c e s s f u l l y m a t c h e d . T h i s m a r k m a y b e g i v e n a n a m e . A l a t e r ( * S K I P )
p a t t e r n w i l l t h e n s k i p f o r w a r d t o t h a t p o i n t i f b a c k t r a c k e d i n t o o n f a i l u r e . A n y n u m b e r o f ( * M A R K )
p a t t e r n s a r e a l l o w e d , a n d t h e N A M E p o r t i o n m a y b e d u p l i c a t e d .
I n a d d i t i o n t o i n t e r a c t i n g w i t h t h e ( * S K I P )
p a t t e r n , ( * M A R
K : N A M E )
c a n b e u s e d t o " l a b e l " a p a t t e r n b r a n c h , s o t h a t a f t e r m a t c h i n g , t h e p r o g r a m c a n d e t e r m i n e w h i c h b r a n c h e s o f t h e p a t t e r n w e r e i n v o l v e d i n t h e m a t c h .
W h e n a m a t c h i s s u c c e s s f u l , t h e $ R E G M A R K
v a r i a b l e w i l l b e s e t t o t h e n a m e o f t h e m o s t r e c e n t l y e x e c u t e d ( * M A R K : N A M E )
t h a t w a s i n v o l v e d i n t h e m a t c h .
T h i s c a n b e u s e d t o d e t e r m i n e w h i c h b r a n c h o f a p a t t e r n w a s m a t c h e d w i t h o u t u s i n g a s e p a r a t e c a p t u r e g r o u p f o r e a c h b r a n c h , w h i c h i n t u r n c a n r e s u l t i n a p e r f o r m a n c e i m p r o v e m e n t , a s p e r l c a n n o t o p t i m i z e / ( ? : ( x ) | ( y ) | ( z ) ) /
a s e f f i c i e n t l y a s s o m e t h i n g l i k e / ( ? : x ( *
M A R K : x ) | y ( * M A R K : y ) | z ( * M A R K : z ) )
/
.
W h e n a m a t c h h a s f a i l e d , a n d u n l e s s a n o t h e r v e r b h a s b e e n i n v o l v e d i n f a i l i n g t h e m a t c h a n d h a s p r o v i d e d i t s o w n n a m e t o u s e , t h e $ R E G E R R O R
v a r i a b l e w i l l b e s e t t o t h e n a m e o f t h e m o s t r e c e n t l y e x e c u t e d ( * M A R K : N A M E )
.
S e e " ( * S K I P ) " f o r m o r e d e t a i l s .
A s a s h o r t c u t ( * M A R K : N A M E )
c a n b e w r i t t e n ( * : N A M E )
.
# ( * T H E N )
( * T H E N : N A M E )
T h i s i s s i m i l a r t o t h e " c u t g r o u p " o p e r a t o r : :
f r o m P e r l 6 . L i k e ( * P R U N E )
, t h i s v e r b a l w a y s m a t c h e s , a n d w h e n b a c k t r a c k e d i n t o o n f a i l u r e , i t c a u s e s t h e r e g e x e n g i n e t o t r y t h e n e x t a l t e r n a t i o n i n t h e i n n e r m o s t e n c l o s i n g g r o u p ( c a p t u r i n g o r o t h e r w i s e ) t h a t h a s a l t e r n a t i o n s . T h e t w o b r a n c h e s o f a ( ? ( c o n d i t i o n ) y e s - p a t t e r
n | n o - p a t t e r n )
d o n o t c o u n t a s a n a l t e r n a t i o n , a s f a r a s ( *
T H E N )
i s c o n c e r n e d .
I t s n a m e c o m e s f r o m t h e o b s e r v a t i o n t h a t t h i s o p e r a t i o n c o m b i n e d w i t h t h e a l t e r n a t i o n o p e r a t o r ( |
) c a n b e u s e d t o c r e a t e w h a t i s e s s e n t i a l l y a p a t t e r n - b a s e d i f / t h e n / e l s e b l o c k :
( COND (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ )
N o t e t h a t i f t h i s o p e r a t o r i s u s e d a n d N O T i n s i d e o f a n a l t e r n a t i o n t h e n i t a c t s e x a c t l y l i k e t h e ( * P R U N E )
o p e r a t o r .
/ A (*PRUNE) B /
i s t h e s a m e a s
/ A (*THEN) B /
b u t
/ ( A (*THEN) B | C (*THEN) D ) /
i s n o t t h e s a m e a s
/ ( A (*PRUNE) B | C (*PRUNE) D ) /
a s a f t e r m a t c h i n g t h e A b u t f a i l i n g o n t h e B t h e ( * T H E N )
v e r b w i l l b a c k t r a c k a n d t r y C ; b u t t h e ( * P R U N E )
v e r b w i l l s i m p l y f a i l .
# V e r b s w i t h o u t a n a r g u m e n t
# ( * C O M M I T )
T h i s i s t h e P e r l 6 " c o m m i t p a t t e r n " < c o m m i t >
or : : :
. I t ' s a z e r o - w i d t h p a t t e r n s i m i l a r t o ( * S K I P )
, e x c e p t t h a t w h e n b a c k t r a c k e d i n t o o n f a i l u r e i t c a u s e s t h e m a t c h t o f a i l o u t r i g h t . N o f u r t h e r a t t e m p t s t o f i n d a v a l i d m a t c h b y a d v a n c i n g t h e s t a r t p o i n t e r w i l l o c c u r a g a i n . F o r e x a m p l e ,
'aaabaaab' =~ /a+b?(*COMMIT)(?{print "$&\n"; $count++})(*FAIL)/;
print "Count=$count\n";
o u t p u t s
aaab
Count=1
I n o t h e r w o r d s , o n c e t h e ( * C O
M M I T )
h a s b e e n e n t e r e d , a n d i f t h e p a t t e r n d o e s n o t m a t c h , t h e r e g e x e n g i n e w i l l n o t t r y a n y f u r t h e r m a t c h i n g o n t h e r e s t o f t h e s t r i n g .
# ( * F A I L )
( * F )
T h i s p a t t e r n m a t c h e s n o t h i n g a n d a l w a y s f a i l s . I t c a n b e u s e d t o f o r c e t h e e n g i n e t o b a c k t r a c k . I t i s e q u i v a l e n t t o ( ?! )
, b u t e a s i e r t o r e a d . I n f a c t , ( ?! )
g e t s o p t i m i s e d i n t o ( * F A I L )
i n t e r n a l l y .
I t i s p r o b a b l y u s e f u l o n l y w h e n c o m b i n e d w i t h ( ? { } )
or ( ?? { } )
.
# ( * A C C E P T )
W A R N I N G : T h i s f e a t u r e i s h i g h l y e x p e r i m e n t a l . I t i s n o t r e c o m m e n d e d f o r p r o d u c t i o n c o d e .
T h i s p a t t e r n m a t c h e s n o t h i n g a n d c a u s e s t h e e n d o f s u c c e s s f u l m a t c h i n g a t t h e p o i n t a t w h i c h t h e ( * A C C E P T )
p a t t e r n w a s e n c o u n t e r e d , r e g a r d l e s s o f w h e t h e r t h e r e i s a c t u a l l y m o r e t o m a t c h i n t h e s t r i n g . W h e n i n s i d e o f a n e s t e d p a t t e r n , s u c h a s r e c u r s i o n , o r i n a s u b p a t t e r n d y n a m i c a l l y g e n e r a t e d v i a ( ?? { } )
, o n l y t h e i n n e r m o s t p a t t e r n i s e n d e d i m m e d i a t e l y .
I f t h e ( * A C C E P T )
i s i n s i d e o f c a p t u r i n g g r o u p s t h e n t h e g r o u p s a r e m a r k e d a s e n d e d a t t h e p o i n t a t w h i c h t h e ( * A C C E P
T )
w a s e n c o u n t e r e d . F o r i n s t a n c e :
'AB' =~ /(A (A|B(*ACCEPT)|C) D)(E )/x;
w i l l m a t c h , a n d $ 1
w i l l b e AB
a n d $ 2
w i l l b e B
, $ 3
w i l l n o t b e s e t . I f a n o t h e r b r a n c h i n t h e i n n e r p a r e n t h e s e s w a s m a t c h e d , s u c h a s i n t h e s t r i n g ' A C D E ' , t h e n t h e D
a n d E
w o u l d h a v e t o b e m a t c h e d a s w e l l .
# B a c k t r a c k i n g
N O T E : T h i s s e c t i o n p r e s e n t s a n a b s t r a c t a p p r o x i m a t i o n o f r e g u l a r e x p r e s s i o n b e h a v i o r . F o r a m o r e r i g o r o u s ( a n d c o m p l i c a t e d ) v i e w o f t h e r u l e s i n v o l v e d i n s e l e c t i n g a m a t c h a m o n g p o s s i b l e a l t e r n a t i v e s , s e e " C o m b i n i n g R E P i e c e s " .
A f u n d a m e n t a l f e a t u r e o f r e g u l a r e x p r e s s i o n m a t c h i n g i n v o l v e s t h e n o t i o n c a l l e d b a c k t r a c k i n g , w h i c h i s c u r r e n t l y u s e d ( w h e n n e e d e d ) b y a l l r e g u l a r n o n - p o s s e s s i v e e x p r e s s i o n q u a n t i f i e r s , n a m e l y *
, * ?
, +
, + ?
, { n , m }
, a n d { n , m } ?
. B a c k t r a c k i n g i s o f t e n o p t i m i z e d i n t e r n a l l y , b u t t h e g e n e r a l p r i n c i p l e o u t l i n e d h e r e i s v a l i d .
F o r a r e g u l a r e x p r e s s i o n t o m a t c h , t h e e n t i r e r e g u l a r e x p r e s s i o n m u s t m a t c h , n o t j u s t p a r t o f i t . S o i f t h e b e g i n n i n g o f a p a t t e r n c o n t a i n i n g a q u a n t i f i e r s u c c e e d s i n a w a y t h a t c a u s e s l a t e r p a r t s i n t h e p a t t e r n t o f a i l , t h e m a t c h i n g e n g i n e b a c k s u p a n d r e c a l c u l a t e s t h e b e g i n n i n g p a r t - - t h a t ' s w h y i t ' s c a l l e d b a c k t r a c k i n g .
H e r e i s a n e x a m p l e o f b a c k t r a c k i n g : L e t ' s s a y y o u w a n t t o f i n d t h e w o r d f o l l o w i n g " f o o " i n t h e s t r i n g " F o o d i s o n t h e f o o t a b l e . " :
$_ = "Food is on the foo table.";
if ( /\b(foo)\s+(\w+)/i ) {
print "$2 follows $1.\n";
}
W h e n t h e m a t c h r u n s , t h e f i r s t p a r t o f t h e r e g u l a r e x p r e s s i o n ( \
b ( f o o )
) f i n d s a p o s s i b l e m a t c h r i g h t a t t h e b e g i n n i n g o f t h e s t r i n g , a n d l o a d s u p $ 1 w i t h " F o o " . H o w e v e r , a s s o o n a s t h e m a t c h i n g e n g i n e s e e s t h a t t h e r e ' s n o w h i t e s p a c e f o l l o w i n g t h e " F o o " t h a t i t h a d s a v e d i n $ 1 , i t r e a l i z e s i t s m i s t a k e a n d s t a r t s o v e r a g a i n o n e c h a r a c t e r a f t e r w h e r e i t h a d t h e t e n t a t i v e m a t c h . T h i s t i m e i t g o e s a l l t h e w a y u n t i l t h e n e x t o c c u r r e n c e o f " f o o " . T h e c o m p l e t e r e g u l a r e x p r e s s i o n m a t c h e s t h i s t i m e , a n d y o u g e t t h e e x p e c t e d o u t p u t o f " t a b l e f o l l o w s f o o . "
S o m e t i m e s m i n i m a l m a t c h i n g c a n h e l p a l o t . I m a g i n e y o u ' d l i k e t o m a t c h e v e r y t h i n g b e t w e e n " f o o " a n d " b a r " . I n i t i a l l y , y o u w r i t e s o m e t h i n g l i k e t h i s :
$_ = "The food is under the bar in the barn.";
if ( /foo(.*)bar/ ) {
print "got <$1>\n";
}
W h i c h p e r h a p s u n e x p e c t e d l y y i e l d s :
got <d is under the bar in the >
T h a t ' s b e c a u s e . *
w a s g r e e d y , s o y o u g e t e v e r y t h i n g b e t w e e n t h e f i r s t " f o o " a n d t h e l a s t " b a r " . H e r e i t ' s m o r e e f f e c t i v e t o u s e m i n i m a l m a t c h i n g t o m a k e s u r e y o u g e t t h e t e x t b e t w e e n a " f o o " a n d t h e f i r s t " b a r " t h e r e a f t e r .
if ( /foo(.*?)bar/ ) { print "got <$1>\n" }
got <d is under the >
H e r e ' s a n o t h e r e x a m p l e . L e t ' s s a y y o u ' d l i k e t o m a t c h a n u m b e r a t t h e e n d o f a s t r i n g , a n d y o u a l s o w a n t t o k e e p t h e p r e c e d i n g p a r t o f t h e m a t c h . S o y o u w r i t e t h i s :
$_ = "I have 2 numbers: 53147";
if ( /(.*)(\d*)/ ) { # Wrong!
print "Beginning is <$1>, number is <$2>.\n";
}
T h a t w o n ' t w o r k a t a l l , b e c a u s e .
*
w a s g r e e d y a n d g o b b l e d u p t h e w h o l e s t r i n g . A s \ d *
c a n m a t c h o n a n e m p t y s t r i n g t h e c o m p l e t e r e g u l a r e x p r e s s i o n m a t c h e d s u c c e s s f u l l y .
Beginning is <I have 2 numbers: 53147>, number is <>.
H e r e a r e s o m e v a r i a n t s , m o s t o f w h i c h d o n ' t w o r k :
$_ = "I have 2 numbers: 53147";
@pats = qw{
(.*)(\d*)
(.*)(\d+)
(.*?)(\d*)
(.*?)(\d+)
(.*)(\d+)$
(.*?)(\d+)$
(.*)\b(\d+)$
(.*\D)(\d+)$
};
for $pat (@pats) {
printf "%-12s ", $pat;
if ( /$pat/ ) {
print "<$1> <$2>\n";
} else {
print "FAIL\n";
}
}
T h a t w i l l p r i n t o u t :
(.*)(\d*) <I have 2 numbers: 53147> <>
(.*)(\d+) <I have 2 numbers: 5314> <7>
(.*?)(\d*) <> <>
(.*?)(\d+) <I have > <2>
(.*)(\d+)$ <I have 2 numbers: 5314> <7>
(.*?)(\d+)$ <I have 2 numbers: > <53147>
(.*)\b(\d+)$ <I have 2 numbers: > <53147>
(.*\D)(\d+)$ <I have 2 numbers: > <53147>
A s y o u s e e , t h i s c a n b e a b i t t r i c k y . I t ' s i m p o r t a n t t o r e a l i z e t h a t a r e g u l a r e x p r e s s i o n i s m e r e l y a s e t o f a s s e r t i o n s t h a t g i v e s a d e f i n i t i o n o f s u c c e s s . T h e r e m a y b e 0 , 1 , o r s e v e r a l d i f f e r e n t w a y s t h a t t h e d e f i n i t i o n m i g h t s u c c e e d a g a i n s t a p a r t i c u l a r s t r i n g . A n d i f t h e r e a r e m u l t i p l e w a y s i t m i g h t s u c c e e d , y o u n e e d t o u n d e r s t a n d b a c k t r a c k i n g t o k n o w w h i c h v a r i e t y o f s u c c e s s y o u w i l l a c h i e v e .
W h e n u s i n g l o o k - a h e a d a s s e r t i o n s a n d n e g a t i o n s , t h i s c a n a l l g e t e v e n t r i c k i e r . I m a g i n e y o u ' d l i k e t o f i n d a s e q u e n c e o f n o n - d i g i t s n o t f o l l o w e d b y " 1 2 3 " . Y o u m i g h t t r y t o w r i t e t h a t a s
$_ = "ABC123";
if ( /^\D*(?!123)/ ) { # Wrong!
print "Yup, no 123 in $_\n";
}
B u t t h a t i s n ' t g o i n g t o m a t c h ; a t l e a s t , n o t t h e w a y y o u ' r e h o p i n g . I t c l a i m s t h a t t h e r e i s n o 1 2 3 i n t h e s t r i n g . H e r e ' s a c l e a r e r p i c t u r e o f w h y t h a t p a t t e r n m a t c h e s , c o n t r a r y t o p o p u l a r e x p e c t a t i o n s :
$x = 'ABC123';
$y = 'ABC445';
print "1: got $1\n" if $x =~ /^(ABC)(?!123)/;
print "2: got $1\n" if $y =~ /^(ABC)(?!123)/;
print "3: got $1\n" if $x =~ /^(\D*)(?!123)/;
print "4: got $1\n" if $y =~ /^(\D*)(?!123)/;
T h i s p r i n t s
2: got ABC
3: got AB
4: got ABC
Y o u m i g h t h a v e e x p e c t e d t e s t 3 t o f a i l b e c a u s e i t s e e m s t o a m o r e g e n e r a l p u r p o s e v e r s i o n o f t e s t 1 . T h e i m p o r t a n t d i f f e r e n c e b e t w e e n t h e m i s t h a t t e s t 3 c o n t a i n s a q u a n t i f i e r ( \ D *
) a n d s o c a n u s e b a c k t r a c k i n g , w h e r e a s t e s t 1 w i l l n o t . W h a t ' s h a p p e n i n g i s t h a t y o u ' v e a s k e d " I s i t t r u e t h a t a t t h e s t a r t o f $ x , f o l l o w i n g 0 o r m o r e n o n - d i g i t s , y o u h a v e s o m e t h i n g t h a t ' s n o t 1 2 3 ? " I f t h e p a t t e r n m a t c h e r h a d l e t \ D *
e x p a n d t o " A B C " , t h i s w o u l d h a v e c a u s e d t h e w h o l e p a t t e r n t o f a i l .
T h e s e a r c h e n g i n e w i l l i n i t i a l l y m a t c h \ D *
w i t h " A B C " . T h e n i t w i l l t r y t o m a t c h ( ?! 1 2 3 )
w i t h " 1 2 3 " , w h i c h f a i l s . B u t b e c a u s e a q u a n t i f i e r ( \ D *
) h a s b e e n u s e d i n t h e r e g u l a r e x p r e s s i o n , t h e s e a r c h e n g i n e c a n b a c k t r a c k a n d r e t r y t h e m a t c h d i f f e r e n t l y i n t h e h o p e o f m a t c h i n g t h e c o m p l e t e r e g u l a r e x p r e s s i o n .
T h e p a t t e r n r e a l l y , r e a l l y w a n t s t o s u c c e e d , s o i t u s e s t h e s t a n d a r d p a t t e r n b a c k - o f f - a n d - r e t r y a n d l e t s \ D *
e x p a n d t o j u s t " A B " t h i s t i m e . N o w t h e r e ' s i n d e e d s o m e t h i n g f o l l o w i n g " A B " t h a t i s n o t " 1 2 3 " . I t ' s " C 1 2 3 " , w h i c h s u f f i c e s .
W e c a n d e a l w i t h t h i s b y u s i n g b o t h a n a s s e r t i o n a n d a n e g a t i o n . W e ' l l s a y t h a t t h e f i r s t p a r t i n $ 1 m u s t b e f o l l o w e d b o t h b y a d i g i t a n d b y s o m e t h i n g t h a t ' s n o t " 1 2 3 " . R e m e m b e r t h a t t h e l o o k - a h e a d s a r e z e r o - w i d t h e x p r e s s i o n s - - t h e y o n l y l o o k , b u t d o n ' t c o n s u m e a n y o f t h e s t r i n g i n t h e i r m a t c h . S o r e w r i t i n g t h i s w a y p r o d u c e s w h a t y o u ' d e x p e c t ; t h a t i s , c a s e 5 w i l l f a i l , b u t c a s e 6 s u c c e e d s :
print "5: got $1\n" if $x =~ /^(\D*)(?=\d)(?!123)/;
print "6: got $1\n" if $y =~ /^(\D*)(?=\d)(?!123)/;
6: got ABC
I n o t h e r w o r d s , t h e t w o z e r o - w i d t h a s s e r t i o n s n e x t t o e a c h o t h e r w o r k a s t h o u g h t h e y ' r e A N D e d t o g e t h e r , j u s t a s y o u ' d u s e a n y b u i l t - i n a s s e r t i o n s : / ^ $ /
m a t c h e s o n l y i f y o u ' r e a t t h e b e g i n n i n g o f t h e l i n e A N D t h e e n d o f t h e l i n e s i m u l t a n e o u s l y . T h e d e e p e r u n d e r l y i n g t r u t h i s t h a t j u x t a p o s i t i o n i n r e g u l a r e x p r e s s i o n s a l w a y s m e a n s A N D , e x c e p t w h e n y o u w r i t e a n e x p l i c i t O R u s i n g t h e v e r t i c a l b a r . / a b /
m e a n s m a t c h " a " A N D ( t h e n ) m a t c h " b " , a l t h o u g h t h e a t t e m p t e d m a t c h e s a r e m a d e a t d i f f e r e n t p o s i t i o n s b e c a u s e " a " i s n o t a z e r o - w i d t h a s s e r t i o n , b u t a o n e - w i d t h a s s e r t i o n .
W A R N I N G : P a r t i c u l a r l y c o m p l i c a t e d r e g u l a r e x p r e s s i o n s c a n t a k e e x p o n e n t i a l t i m e t o s o l v e b e c a u s e o f t h e i m m e n s e n u m b e r o f p o s s i b l e w a y s t h e y c a n u s e b a c k t r a c k i n g t o t r y f o r a m a t c h . F o r e x a m p l e , w i t h o u t i n t e r n a l o p t i m i z a t i o n s d o n e b y t h e r e g u l a r e x p r e s s i o n e n g i n e , t h i s w i l l t a k e a p a i n f u l l y l o n g t i m e t o r u n :
'aaaaaaaaaaaa' =~ /((a{0,5}){0,5})*[c ]/
A n d i f y o u u s e d *
' s i n t h e i n t e r n a l g r o u p s i n s t e a d o f l i m i t i n g t h e m t o 0 t h r o u g h 5 m a t c h e s , t h e n i t w o u l d t a k e f o r e v e r - - o r u n t i l y o u r a n o u t o f s t a c k s p a c e . M o r e o v e r , t h e s e i n t e r n a l o p t i m i z a t i o n s a r e n o t a l w a y s a p p l i c a b l e . F o r e x a m p l e , i f y o u p u t { 0 , 5 }
i n s t e a d o f *
o n t h e e x t e r n a l g r o u p , n o c u r r e n t o p t i m i z a t i o n i s a p p l i c a b l e , a n d t h e m a t c h t a k e s a l o n g t i m e t o f i n i s h .
A p o w e r f u l t o o l f o r o p t i m i z i n g s u c h b e a s t s i s w h a t i s k n o w n a s a n " i n d e p e n d e n t g r o u p " , w h i c h d o e s n o t b a c k t r a c k ( s e e " ( ? > p a t t e r n )
" ) . N o t e a l s o t h a t z e r o - l e n g t h l o o k - a h e a d / l o o k - b e h i n d a s s e r t i o n s w i l l n o t b a c k t r a c k t o m a k e t h e t a i l m a t c h , s i n c e t h e y a r e i n " l o g i c a l " c o n t e x t : o n l y w h e t h e r t h e y m a t c h i s c o n s i d e r e d r e l e v a n t . F o r a n e x a m p l e w h e r e s i d e - e f f e c t s o f l o o k - a h e a d m i g h t h a v e i n f l u e n c e d t h e f o l l o w i n g m a t c h , s e e " ( ? > p a t t e r n )
" .
# V e r s i o n 8 R e g u l a r E x p r e s s i o n s
I n c a s e y o u ' r e n o t f a m i l i a r w i t h t h e " r e g u l a r " V e r s i o n 8 r e g e x r o u t i n e s , h e r e a r e t h e p a t t e r n - m a t c h i n g r u l e s n o t d e s c r i b e d a b o v e .
A n y s i n g l e c h a r a c t e r m a t c h e s i t s e l f , u n l e s s i t i s a m e t a c h a r a c t e r w i t h a s p e c i a l m e a n i n g d e s c r i b e d h e r e o r a b o v e . Y o u c a n c a u s e c h a r a c t e r s t h a t n o r m a l l y f u n c t i o n a s m e t a c h a r a c t e r s t o b e i n t e r p r e t e d l i t e r a l l y b y p r e f i x i n g t h e m w i t h a " \ " ( e . g . , " \ . " m a t c h e s a " . " , n o t a n y c h a r a c t e r ; " \ \ " m a t c h e s a " \ " ) . T h i s e s c a p e m e c h a n i s m i s a l s o r e q u i r e d f o r t h e c h a r a c t e r u s e d a s t h e p a t t e r n d e l i m i t e r .
A s e r i e s o f c h a r a c t e r s m a t c h e s t h a t s e r i e s o f c h a r a c t e r s i n t h e t a r g e t s t r i n g , s o t h e p a t t e r n b l
u r f l
w o u l d m a t c h " b l u r f l " i n t h e t a r g e t s t r i n g .
Y o u c a n s p e c i f y a c h a r a c t e r c l a s s , b y e n c l o s i n g a l i s t o f c h a r a c t e r s i n [ ]
, w h i c h w i l l m a t c h a n y c h a r a c t e r f r o m t h e l i s t . I f t h e f i r s t c h a r a c t e r a f t e r t h e " [ " i s " ^ " , t h e c l a s s m a t c h e s a n y c h a r a c t e r n o t i n t h e l i s t . W i t h i n a l i s t , t h e " - " c h a r a c t e r s p e c i f i e s a r a n g e , s o t h a t a - z
r e p r e s e n t s a l l c h a r a c t e r s b e t w e e n " a " a n d " z " , i n c l u s i v e . I f y o u w a n t e i t h e r " - " o r " ] " i t s e l f t o b e a m e m b e r o f a c l a s s , p u t i t a t t h e s t a r t o f t h e l i s t ( p o s s i b l y a f t e r a " ^ " ) , o r e s c a p e i t w i t h a b a c k s l a s h . " - " i s a l s o t a k e n l i t e r a l l y w h e n i t i s a t t h e e n d o f t h e l i s t , j u s t b e f o r e t h e c l o s i n g " ] " . ( T h e f o l l o w i n g a l l s p e c i f y t h e s a m e c l a s s o f t h r e e c h a r a c t e r s : [ - a z ]
, [ a z - ]
, a n d [ a \ - z ]
. A l l a r e d i f f e r e n t f r o m [ a - z ]
, w h i c h s p e c i f i e s a c l a s s c o n t a i n i n g t w e n t y - s i x c h a r a c t e r s , e v e n o n E B C D I C - b a s e d c h a r a c t e r s e t s . ) A l s o , i f y o u t r y t o u s e t h e c h a r a c t e r c l a s s e s \ w
, \ W
, \ s
, \ S
, \ d
, o r \ D
a s e n d p o i n t s o f a r a n g e , t h e " - " i s u n d e r s t o o d l i t e r a l l y .
N o t e a l s o t h a t t h e w h o l e r a n g e i d e a i s r a t h e r u n p o r t a b l e b e t w e e n c h a r a c t e r s e t s - - a n d e v e n w i t h i n c h a r a c t e r s e t s t h e y m a y c a u s e r e s u l t s y o u p r o b a b l y d i d n ' t e x p e c t . A s o u n d p r i n c i p l e i s t o u s e o n l y r a n g e s t h a t b e g i n f r o m a n d e n d a t e i t h e r a l p h a b e t i c s o f e q u a l c a s e ( [ a - e ] , [ A - E ] ) , o r d i g i t s ( [ 0 - 9 ] ) . A n y t h i n g e l s e i s u n s a f e . I f i n d o u b t , s p e l l o u t t h e c h a r a c t e r s e t s i n f u l l .
C h a r a c t e r s m a y b e s p e c i f i e d u s i n g a m e t a c h a r a c t e r s y n t a x m u c h l i k e t h a t u s e d i n C : " \ n " m a t c h e s a n e w l i n e , " \ t " a t a b , " \ r " a c a r r i a g e r e t u r n , " \ f " a f o r m f e e d , e t c . M o r e g e n e r a l l y , \ n n n , w h e r e n n n i s a s t r i n g o f t h r e e o c t a l d i g i t s , m a t c h e s t h e c h a r a c t e r w h o s e c o d e d c h a r a c t e r s e t v a l u e i s n n n . S i m i l a r l y , \ x nn , w h e r e nn a r e h e x a d e c i m a l d i g i t s , m a t c h e s t h e c h a r a c t e r w h o s e o r d i n a l i s nn . T h e e x p r e s s i o n \ c x m a t c h e s t h e c h a r a c t e r c o n t r o l - x . F i n a l l y , t h e " . " m e t a c h a r a c t e r m a t c h e s a n y c h a r a c t e r e x c e p t " \ n " ( u n l e s s y o u u s e / s
) .
Y o u c a n s p e c i f y a s e r i e s o f a l t e r n a t i v e s f o r a p a t t e r n u s i n g " | " t o s e p a r a t e t h e m , s o t h a t f e e | f
i e | f o e
w i l l m a t c h a n y o f " f e e " , " f i e " , o r " f o e " i n t h e t a r g e t s t r i n g ( a s w o u l d f ( e | i | o ) e
) . T h e f i r s t a l t e r n a t i v e i n c l u d e s e v e r y t h i n g f r o m t h e l a s t p a t t e r n d e l i m i t e r ( " ( " , " ( ? : " , e t c . o r t h e b e g i n n i n g o f t h e p a t t e r n ) u p t o t h e f i r s t " | " , a n d t h e l a s t a l t e r n a t i v e c o n t a i n s e v e r y t h i n g f r o m t h e l a s t " | " t o t h e n e x t c l o s i n g p a t t e r n d e l i m i t e r . T h a t ' s w h y i t ' s c o m m o n p r a c t i c e t o i n c l u d e a l t e r n a t i v e s i n p a r e n t h e s e s : t o m i n i m i z e c o n f u s i o n a b o u t w h e r e t h e y s t a r t a n d e n d .
A l t e r n a t i v e s a r e t r i e d f r o m l e f t t o r i g h t , s o t h e f i r s t a l t e r n a t i v e f o u n d f o r w h i c h t h e e n t i r e e x p r e s s i o n m a t c h e s , i s t h e o n e t h a t i s c h o s e n . T h i s m e a n s t h a t a l t e r n a t i v e s a r e n o t n e c e s s a r i l y g r e e d y . F o r e x a m p l e : w h e n m a t c h i n g f o o | f o o t
a g a i n s t " b a r e f o o t " , o n l y t h e " f o o " p a r t w i l l m a t c h , a s t h a t i s t h e f i r s t a l t e r n a t i v e t r i e d , a n d i t s u c c e s s f u l l y m a t c h e s t h e t a r g e t s t r i n g . ( T h i s m i g h t n o t s e e m i m p o r t a n t , b u t i t i s i m p o r t a n t w h e n y o u a r e c a p t u r i n g m a t c h e d t e x t u s i n g p a r e n t h e s e s . )
A l s o r e m e m b e r t h a t " | " i s i n t e r p r e t e d a s a l i t e r a l w i t h i n s q u a r e b r a c k e t s , s o i f y o u w r i t e [ f e e |
f i e | f o e ]
y o u ' r e r e a l l y o n l y m a t c h i n g [ f e i o | ]
.
W i t h i n a p a t t e r n , y o u m a y d e s i g n a t e s u b p a t t e r n s f o r l a t e r r e f e r e n c e b y e n c l o s i n g t h e m i n p a r e n t h e s e s , a n d y o u m a y r e f e r b a c k t o t h e n t h s u b p a t t e r n l a t e r i n t h e p a t t e r n u s i n g t h e m e t a c h a r a c t e r \ n o r \ g n . S u b p a t t e r n s a r e n u m b e r e d b a s e d o n t h e l e f t t o r i g h t o r d e r o f t h e i r o p e n i n g p a r e n t h e s i s . A b a c k r e f e r e n c e m a t c h e s w h a t e v e r a c t u a l l y m a t c h e d t h e s u b p a t t e r n i n t h e s t r i n g b e i n g e x a m i n e d , n o t t h e r u l e s f o r t h a t s u b p a t t e r n . T h e r e f o r e , ( 0 | 0 x ) \ d * \ s \ g 1 \ d *
w i l l m a t c h " 0 x 1 2 3 4 0 x 4 3 2 1 " , b u t n o t " 0 x 1 2 3 4 0 1 2 3 4 " , b e c a u s e s u b p a t t e r n 1 m a t c h e d " 0 x " , e v e n t h o u g h t h e r u l e 0 | 0 x
c o u l d p o t e n t i a l l y m a t c h t h e l e a d i n g 0 i n t h e s e c o n d n u m b e r .
# W a r n i n g o n \ 1 I n s t e a d o f $ 1
S o m e p e o p l e g e t t o o u s e d t o w r i t i n g t h i n g s l i k e :
$pattern =~ s/(\W)/\\\1/g;
T h i s i s g r a n d f a t h e r e d ( f o r \ 1 t o \ 9 ) f o r t h e R H S o f a s u b s t i t u t e t o a v o i d s h o c k i n g t h e s e d a d d i c t s , b u t i t ' s a d i r t y h a b i t t o g e t i n t o . T h a t ' s b e c a u s e i n P e r l T h i n k , t h e r i g h t h a n d s i d e o f a n s /
/ /
i s a d o u b l e - q u o t e d s t r i n g . \ 1
i n t h e u s u a l d o u b l e - q u o t e d s t r i n g m e a n s a c o n t r o l - A . T h e c u s t o m a r y U n i x m e a n i n g o f \ 1
i s k l u d g e d i n f o r s / / /
. H o w e v e r , i f y o u g e t i n t o t h e h a b i t o f d o i n g t h a t , y o u g e t y o u r s e l f i n t o t r o u b l e i f y o u t h e n a d d a n / e
m o d i f i e r .
s/(\d+)/ \1 + 1 /eg; # causes warning under -w
O r i f y o u t r y t o d o
s/(\d+)/\1000/;
Y o u c a n ' t d i s a m b i g u a t e t h a t b y s a y i n g \ { 1 } 0 0 0
, w h e r e a s y o u c a n f i x i t w i t h $ { 1 } 0 0 0
. T h e o p e r a t i o n o f i n t e r p o l a t i o n s h o u l d n o t b e c o n f u s e d w i t h t h e o p e r a t i o n o f m a t c h i n g a b a c k r e f e r e n c e . C e r t a i n l y t h e y m e a n t w o d i f f e r e n t t h i n g s o n t h e l e f t s i d e o f t h e s / / /
.
# R e p e a t e d P a t t e r n s M a t c h i n g a Z e r o - l e n g t h S u b s t r i n g
W A R N I N G : D i f f i c u l t m a t e r i a l ( a n d p r o s e ) a h e a d . T h i s s e c t i o n n e e d s a r e w r i t e .
R e g u l a r e x p r e s s i o n s p r o v i d e a t e r s e a n d p o w e r f u l p r o g r a m m i n g l a n g u a g e . A s w i t h m o s t o t h e r p o w e r t o o l s , p o w e r c o m e s t o g e t h e r w i t h t h e a b i l i t y t o w r e a k h a v o c .
A c o m m o n a b u s e o f t h i s p o w e r s t e m s f r o m t h e a b i l i t y t o m a k e i n f i n i t e l o o p s u s i n g r e g u l a r e x p r e s s i o n s , w i t h s o m e t h i n g a s i n n o c u o u s a s :
'foo' =~ m{ ( o? )* }x;
T h e o ?
m a t c h e s a t t h e b e g i n n i n g o f ' f o o '
, a n d s i n c e t h e p o s i t i o n i n t h e s t r i n g i s n o t m o v e d b y t h e m a t c h , o ?
w o u l d m a t c h a g a i n a n d a g a i n b e c a u s e o f t h e *
q u a n t i f i e r . A n o t h e r c o m m o n w a y t o c r e a t e a s i m i l a r c y c l e i s w i t h t h e l o o p i n g m o d i f i e r / / g
:
@matches = ( 'foo' =~ m{ o? }xg );
or
print "match: <$&>\n" while 'foo' =~ m{ o? }xg;
o r t h e l o o p i m p l i e d b y s p l i t ( ) .
H o w e v e r , l o n g e x p e r i e n c e h a s s h o w n t h a t m a n y p r o g r a m m i n g t a s k s m a y b e s i g n i f i c a n t l y s i m p l i f i e d b y u s i n g r e p e a t e d s u b e x p r e s s i o n s t h a t m a y m a t c h z e r o - l e n g t h s u b s t r i n g s . H e r e ' s a s i m p l e e x a m p l e b e i n g :
@chars = split //, $string; # // is not magic in split
($whitewashed = $string) =~ s/()/ /g; # parens avoid magic s// /
T h u s P e r l a l l o w s s u c h c o n s t r u c t s , b y f o r c e f u l l y b r e a k i n g t h e i n f i n i t e l o o p . T h e r u l e s f o r t h i s a r e d i f f e r e n t f o r l o w e r - l e v e l l o o p s g i v e n b y t h e g r e e d y q u a n t i f i e r s * + { }
, a n d f o r h i g h e r - l e v e l o n e s l i k e t h e / g
m o d i f i e r o r s p l i t ( ) o p e r a t o r .
T h e l o w e r - l e v e l l o o p s a r e i n t e r r u p t e d ( t h a t i s , t h e l o o p i s b r o k e n ) w h e n P e r l d e t e c t s t h a t a r e p e a t e d e x p r e s s i o n m a t c h e d a z e r o - l e n g t h s u b s t r i n g . T h u s
m{ (?: NON_ZERO_LENGTH | ZERO_LENGTH )* }x;
i s m a d e e q u i v a l e n t t o
m{ (?: NON_ZERO_LENGTH )* (?: ZERO_LENGTH )? }x;
F o r e x a m p l e , t h i s p r o g r a m
#!perl -l
"aaaaab" =~ /
(?:
a # non-zero
| # or
(?{print "hello"}) # print hello whenever this
# branch is tried
(?=(b )) # zero-width assertion
)* # any number of times
/x;
print $&;
print $1;
p r i n t s
hello
aaaaa
b
N o t i c e t h a t " h e l l o " i s o n l y p r i n t e d o n c e , a s w h e n P e r l s e e s t h a t t h e s i x t h i t e r a t i o n o f t h e o u t e r m o s t ( ? : ) *
m a t c h e s a z e r o - l e n g t h s t r i n g , i t s t o p s t h e *
.
T h e h i g h e r - l e v e l l o o p s p r e s e r v e a n a d d i t i o n a l s t a t e b e t w e e n i t e r a t i o n s : w h e t h e r t h e l a s t m a t c h w a s z e r o - l e n g t h . T o b r e a k t h e l o o p , t h e f o l l o w i n g m a t c h a f t e r a z e r o - l e n g t h m a t c h i s p r o h i b i t e d t o h a v e a l e n g t h o f z e r o . T h i s p r o h i b i t i o n i n t e r a c t s w i t h b a c k t r a c k i n g ( s e e " B a c k t r a c k i n g " ) , a n d s o t h e s e c o n d b e s t m a t c h i s c h o s e n i f t h e b e s t m a t c h i s o f z e r o l e n g t h .
F o r e x a m p l e :
$_ = 'bar';
s/\w??/<$&>/g;
r e s u l t s i n < > < b > < > < a > < > < r > < >
. A t e a c h p o s i t i o n o f t h e s t r i n g t h e b e s t m a t c h g i v e n b y n o n - g r e e d y ??
i s t h e z e r o - l e n g t h m a t c h , a n d t h e s e c o n d b e s t m a t c h i s w h a t i s m a t c h e d b y \ w
. T h u s z e r o - l e n g t h m a t c h e s a l t e r n a t e w i t h o n e - c h a r a c t e r - l o n g m a t c h e s .
S i m i l a r l y , f o r r e p e a t e d m / ( ) / g
t h e s e c o n d - b e s t m a t c h i s t h e m a t c h a t t h e p o s i t i o n o n e n o t c h f u r t h e r i n t h e s t r i n g .
T h e a d d i t i o n a l s t a t e o f b e i n g m a t c h e d w i t h z e r o - l e n g t h i s a s s o c i a t e d w i t h t h e m a t c h e d s t r i n g , a n d i s r e s e t b y e a c h a s s i g n m e n t t o p o s ( ) . Z e r o - l e n g t h m a t c h e s a t t h e e n d o f t h e p r e v i o u s m a t c h a r e i g n o r e d d u r i n g s p l i t
.
# C o m b i n i n g R E P i e c e s
E a c h o f t h e e l e m e n t a r y p i e c e s o f r e g u l a r e x p r e s s i o n s w h i c h w e r e d e s c r i b e d b e f o r e ( s u c h a s ab
or \ Z
) c o u l d m a t c h a t m o s t o n e s u b s t r i n g a t t h e g i v e n p o s i t i o n o f t h e i n p u t s t r i n g . H o w e v e r , i n a t y p i c a l r e g u l a r e x p r e s s i o n t h e s e e l e m e n t a r y p i e c e s a r e c o m b i n e d i n t o m o r e c o m p l i c a t e d p a t t e r n s u s i n g c o m b i n i n g o p e r a t o r s ST
, S | T
, S *
e t c . ( i n t h e s e e x a m p l e s S
a n d T
a r e r e g u l a r s u b e x p r e s s i o n s ) .
S u c h c o m b i n a t i o n s c a n i n c l u d e a l t e r n a t i v e s , l e a d i n g t o a p r o b l e m o f c h o i c e : i f w e m a t c h a r e g u l a r e x p r e s s i o n a | a b
a g a i n s t " a b c "
, w i l l i t m a t c h s u b s t r i n g " a "
or " a b
"
? O n e w a y t o d e s c r i b e w h i c h s u b s t r i n g i s a c t u a l l y m a t c h e d i s t h e c o n c e p t o f b a c k t r a c k i n g ( s e e " B a c k t r a c k i n g " ) . H o w e v e r , t h i s d e s c r i p t i o n i s t o o l o w - l e v e l a n d m a k e s y o u t h i n k i n t e r m s o f a p a r t i c u l a r i m p l e m e n t a t i o n .
A n o t h e r d e s c r i p t i o n s t a r t s w i t h n o t i o n s o f " b e t t e r " / " w o r s e " . A l l t h e s u b s t r i n g s w h i c h m a y b e m a t c h e d b y t h e g i v e n r e g u l a r e x p r e s s i o n c a n b e s o r t e d f r o m t h e " b e s t " m a t c h t o t h e " w o r s t " m a t c h , a n d i t i s t h e " b e s t " m a t c h w h i c h i s c h o s e n . T h i s s u b s t i t u t e s t h e q u e s t i o n o f " w h a t i s c h o s e n ? " b y t h e q u e s t i o n o f " w h i c h m a t c h e s a r e b e t t e r , a n d w h i c h a r e w o r s e ? " .
A g a i n , f o r e l e m e n t a r y p i e c e s t h e r e i s n o s u c h q u e s t i o n , s i n c e a t m o s t o n e m a t c h a t a g i v e n p o s i t i o n i s p o s s i b l e . T h i s s e c t i o n d e s c r i b e s t h e n o t i o n o f b e t t e r / w o r s e f o r c o m b i n i n g o p e r a t o r s . I n t h e d e s c r i p t i o n b e l o w S
a n d T
a r e r e g u l a r s u b e x p r e s s i o n s .
# ST
C o n s i d e r t w o p o s s i b l e m a t c h e s , AB
a n d A ' B '
, A
a n d A '
a r e s u b s t r i n g s w h i c h c a n b e m a t c h e d b y S
, B
a n d B '
a r e s u b s t r i n g s w h i c h c a n b e m a t c h e d b y T
.
If A
i s a b e t t e r m a t c h f o r S
t h a n A '
, AB
i s a b e t t e r m a t c h t h a n A ' B
'
.
If A
a n d A '
c o i n c i d e : AB
i s a b e t t e r m a t c h t h a n A B '
if B
i s a b e t t e r m a t c h f o r T
t h a n B '
.
# S | T
W h e n S
c a n m a t c h , i t i s a b e t t e r m a t c h t h a n w h e n o n l y T
c a n m a t c h .
O r d e r i n g o f t w o m a t c h e s f o r S
i s t h e s a m e a s f o r S
. S i m i l a r f o r t w o m a t c h e s f o r T
.
# S { R E P E A T _ C O U N T }
M a t c h e s a s S S S . . . S
( r e p e a t e d a s m a n y t i m e s a s n e c e s s a r y ) .
# S { m i n , m a x }
M a t c h e s a s S { m a x } | S { m a x - 1 } | . . . |
S { m i n + 1 } | S { m i n }
.
# S { m i n , m a x } ?
M a t c h e s a s S { m i n } | S { m i n + 1 } | . . . |
S { m a x - 1 } | S { m a x }
.
# S ?
, S *
, S +
S a m e a s S { 0 , 1 }
, S { 0 , B I G _ N U M B E R }
, S { 1 , B I G _ N U M B E R }
r e s p e c t i v e l y .
# S ??
, S * ?
, S + ?
S a m e a s S { 0 , 1 } ?
, S { 0 , B I G _ N U M B E R }
?
, S { 1 , B I G _ N U M B E R } ?
r e s p e c t i v e l y .
# ( ? > S )
M a t c h e s t h e b e s t m a t c h f o r S
a n d o n l y t h a t .
# ( ? = S )
, ( ? < = S )
O n l y t h e b e s t m a t c h f o r S
i s c o n s i d e r e d . ( T h i s i s i m p o r t a n t o n l y i f S
h a s c a p t u r i n g p a r e n t h e s e s , a n d b a c k r e f e r e n c e s a r e u s e d s o m e w h e r e e l s e i n t h e w h o l e r e g u l a r e x p r e s s i o n . )
# ( ?! S )
, ( ? < ! S )
F o r t h i s g r o u p i n g o p e r a t o r t h e r e i s n o n e e d t o d e s c r i b e t h e o r d e r i n g , s i n c e o n l y w h e t h e r o r n o t S
c a n m a t c h i s i m p o r t a n t .
# ( ?? { E X P R } )
, ( ? P A R N O )
T h e o r d e r i n g i s t h e s a m e a s f o r t h e r e g u l a r e x p r e s s i o n w h i c h i s t h e r e s u l t o f E X P R , o r t h e p a t t e r n c o n t a i n e d b y c a p t u r e g r o u p P A R N O .
# ( ? ( c o n d i t i o n ) y e s - p a t t e r n | n o - p a t
t e r n )
R e c a l l t h a t w h i c h o f y e s - p a t t e r
n
or n o - p a t t e r n
a c t u a l l y m a t c h e s i s a l r e a d y d e t e r m i n e d . T h e o r d e r i n g o f t h e m a t c h e s i s t h e s a m e a s f o r t h e c h o s e n s u b e x p r e s s i o n .
T h e a b o v e r e c i p e s d e s c r i b e t h e o r d e r i n g o f m a t c h e s a t a g i v e n p o s i t i o n . O n e m o r e r u l e i s n e e d e d t o u n d e r s t a n d h o w a m a t c h i s d e t e r m i n e d f o r t h e w h o l e r e g u l a r e x p r e s s i o n : a m a t c h a t a n e a r l i e r p o s i t i o n i s a l w a y s b e t t e r t h a n a m a t c h a t a l a t e r p o s i t i o n .
# C r e a t i n g C u s t o m R E E n g i n e s
A s o f P e r l 5 . 1 0 . 0 , o n e c a n c r e a t e c u s t o m r e g u l a r e x p r e s s i o n e n g i n e s . T h i s i s n o t f o r t h e f a i n t o f h e a r t , a s t h e y h a v e t o p l u g i n a t t h e C l e v e l . S e e p e r l r e a p i f o r m o r e d e t a i l s .
A s a n a l t e r n a t i v e , o v e r l o a d e d c o n s t a n t s ( s e e o v e r l o a d ) p r o v i d e a s i m p l e w a y t o e x t e n d t h e f u n c t i o n a l i t y o f t h e R E e n g i n e , b y s u b s t i t u t i n g o n e p a t t e r n f o r a n o t h e r .
S u p p o s e t h a t w e w a n t t o e n a b l e a n e w R E e s c a p e - s e q u e n c e \ Y |
w h i c h m a t c h e s a t a b o u n d a r y b e t w e e n w h i t e s p a c e c h a r a c t e r s a n d n o n - w h i t e s p a c e c h a r a c t e r s . N o t e t h a t (
? = \ S ) ( ? < ! \ S ) | ( ?! \ S ) ( ? < = \ S )
m a t c h e s e x a c t l y a t t h e s e p o s i t i o n s , s o w e w a n t t o h a v e e a c h \ Y |
i n t h e p l a c e o f t h e m o r e c o m p l i c a t e d v e r s i o n . W e c a n c r e a t e a m o d u l e c
u s t o m r e
t o d o t h i s :
package customre;
use overload;
sub import {
shift;
die "No argument to customre::import allowed" if @_;
overload::constant 'qr' => \&convert;
}
sub invalid { die "/$_[0]/: invalid escape '\\$_[1 ]'"}
# We must also take care of not escaping the legitimate \\Y|
# sequence, hence the presence of '\\' in the conversion rules.
my %rules = ( '\\' => '\\\\',
'Y|' => qr/(?=\S)(?<!\S)|(?!\S)(?<=\S)/ );
sub convert {
my $re = shift;
$re =~ s{
\\ ( \\ | Y . )
}
{ $rules{$1} or invalid($re,$1) }sgex;
return $re;
}
N o w u s e c u s t o m r e
e n a b l e s t h e n e w e s c a p e i n c o n s t a n t r e g u l a r e x p r e s s i o n s , i . e . , t h o s e w i t h o u t a n y r u n t i m e v a r i a b l e i n t e r p o l a t i o n s . A s d o c u m e n t e d i n o v e r l o a d , t h i s c o n v e r s i o n w i l l w o r k o n l y o v e r l i t e r a l p a r t s o f r e g u l a r e x p r e s s i o n s . F o r \ Y | $ r e \ Y |
t h e v a r i a b l e p a r t o f t h i s r e g u l a r e x p r e s s i o n n e e d s t o b e c o n v e r t e d e x p l i c i t l y ( b u t o n l y i f t h e s p e c i a l m e a n i n g o f \ Y |
s h o u l d b e e n a b l e d i n s i d e $ r e ) :
use customre;
$re = <>;
chomp $re;
$re = customre::convert $re;
/\Y|$re\Y|/;
# P C R E / P y t h o n S u p p o r t
A s o f P e r l 5 . 1 0 . 0 , P e r l s u p p o r t s s e v e r a l P y t h o n / P C R E - s p e c i f i c e x t e n s i o n s t o t h e r e g e x s y n t a x . W h i l e P e r l p r o g r a m m e r s a r e e n c o u r a g e d t o u s e t h e P e r l - s p e c i f i c s y n t a x , t h e f o l l o w i n g a r e a l s o a c c e p t e d :
# ( ? P < N A M E > p a t t e r n )
D e f i n e a n a m e d c a p t u r e g r o u p . E q u i v a l e n t t o ( ? < N A M E > p a t t e r n )
.
# ( ? P = N A M E )
B a c k r e f e r e n c e t o a n a m e d c a p t u r e g r o u p . E q u i v a l e n t t o \ g { N A M E }
.
# ( ? P > N A M E )
S u b r o u t i n e c a l l t o a n a m e d c a p t u r e g r o u p . E q u i v a l e n t t o ( ? & N A M
E )
.
# B U G S
M a n y r e g u l a r e x p r e s s i o n c o n s t r u c t s d o n ' t w o r k o n E B C D I C p l a t f o r m s .
T h e r e a r e a n u m b e r o f i s s u e s w i t h r e g a r d t o c a s e - i n s e n s i t i v e m a t c h i n g i n U n i c o d e r u l e s . S e e i
u n d e r " M o d i f i e r s " a b o v e .
T h i s d o c u m e n t v a r i e s f r o m d i f f i c u l t t o u n d e r s t a n d t o c o m p l e t e l y a n d u t t e r l y o p a q u e . T h e w a n d e r i n g p r o s e r i d d l e d w i t h j a r g o n i s h a r d t o f a t h o m i n s e v e r a l p l a c e s .
T h i s d o c u m e n t n e e d s a r e w r i t e t h a t s e p a r a t e s t h e t u t o r i a l c o n t e n t f r o m t h e r e f e r e n c e c o n t e n t .
# S E E A L S O
p e r l r e q u i c k .
p e r l r e t u t .
" R e g e x p Q u o t e - L i k e O p e r a t o r s " i n p e r l o p .
" G o r y d e t a i l s o f p a r s i n g q u o t e d c o n s t r u c t s " i n p e r l o p .
p e r l f a q 6 .
" p o s " i n p e r l f u n c .
p e r l l o c a l e .
p e r l e b c d i c .
M a s t e r i n g R e g u l a r E x p r e s s i o n s b y J e f f r e y F r i e d l , p u b l i s h e d b y O ' R e i l l y a n d A s s o c i a t e s .
P e r l d o c B r o w s e r i s m a i n t a i n e d b y D a n B o o k ( D B O O K ) . P l e a s e c o n t a c t h i m v i a t h e G i t H u b i s s u e t r a c k e r or e m a i l r e g a r d i n g a n y i s s u e s w i t h t h e s i t e i t s e l f , s e a r c h , o r r e n d e r i n g o f d o c u m e n t a t i o n .
T h e P e r l d o c u m e n t a t i o n i s m a i n t a i n e d b y t h e P e r l 5 P o r t e r s i n t h e d e v e l o p m e n t o f P e r l . P l e a s e c o n t a c t t h e m v i a t h e P e r l i s s u e t r a c k e r , t h e m a i l i n g l i s t , o r I R C t o r e p o r t a n y i s s u e s w i t h t h e c o n t e n t s o r f o r m a t o f t h e d o c u m e n t a t i o n .