Home  |   Guest Book |    Technical Page |    Personal Page   

RegEx  pattern Matching Quick Reference
 
About Me
Who is a Good QA
Latest News
Testing Concepts
Automation Tools
Agile Development
UNIX Basics
Perl Scripting
Python Scripting
MySQL
Technology
Repository
Imp. Commands
My Resume

 

Pattern Matching

Result

$a =~ /pat/

match

True if $a contains pattern

$a =~ s/p/r/

substitution

Replace contents of p with r in $a

$a =~ tr/a-z/A-Z/

translation

Translate to corresponding characters

Return to Top

Logical Operators

Result

$a && $b

And

True if $a is true and $b is true

$a || $b

Or

$a if $a is true, otherwise $b

! $a

Not

True if $a is not true

Return to Top

Arithmetic Operators

Result

$a + $b

Add

Sum of $a and $b

$a - $b

Subtract

Difference of $a and $b

$a * $b

Multiply

Product of $a times $b

$a / $b

Divide

Quotient of $a divided by $b

$a % $b

Modulus

Remainder of $a divided by $b

$a ** $b

Exponentiate

$a to the power $b

++$a,$a++

Autoincrement

Add 1 to $a

--$a,$a--

Autodecrement

Subtract 1 from $a

rand($a)

Random

A random number in range 0 .. $a

Return to Top

String Operators

Result

$a . $b

Concatenation

Values of $a and $b as one long string

$a x $b

Repeat

Value of $a strung together $b times

substr($a,$o,$l)

Substring

Substring at offset $o of length $l

index($a,$b)

Index

Offset of string $b in string $a

Return to Top

Assignment Operators

Result

$a = $b

Assign

$a gets the value of $b

$a += $b

Add to

Increase $a by $b

$a -= $b

Subtract from

Decrease $a by $b

$a .= $b

Append

Append string $b to $a

Return to Top

File Test Operators

Result

-r $a

Readable

File name in $a is readable by effective uid

-w $a

Writable

Writable by effective uid

-x $a

Executable

Executable by effective uid

-o $a

Owned

Owned by effective uid

-R $a

Readable

Readable by real uid

-W $a

Writable

Writable by real uid

-X $a

Executable

Executable by real uid

-O $a

Owned

Owned by real uid

-e $a

Exists

File exists

-z $a

Non-zero size

File has non-zero size (returns size in bytes)

-s $a

Zero size

File has zero size

-f $a

Regular file

File is a regular file

-d $a

Directory

File is a directory

-l $a

Symbolic link

File is a symbolic link

-p $a

Named pipe

File is a named pipe (FIFO)

-S $a

Socket

File is a socket

-b $a

Block

File is a block special file

-c $a

Character

File is a character special file

-u $a

UID

File has setuid bit set

-g $a

GID

File has setgid bit set

-k $a

Sticky bit

File has sticky bit set

-T $a

Text file

File is a text file

-B $a

Binary

File is a binary file (opposite of -T)

-M $a

Modify

Age of file (at startup) in days since modification

-A $a

Last Access

Age of file (at startup) in days since last access

-C $a

Inode change

Age of file (at startup) in days since inode change

 

 

Specific characters:

\t

A tab character

\n

A newline character (OS neutral)

\r

A carriage return character

\f

A form feed character

\cX

Control character CTRL-X

\NNN

Octal code for character NNN

 

Metacharacters:

The following 12 characters need to be escaped with a backslash - “\” - because by default, they mean something special.

\ | ( ) [ { ^ $ * + ? .

 

.

Match any one character (except \n)

|

Alternation

( )

Group and capture

[]

Define character class

\

Modify the meaning of the next char.

 

 

Anchors:

^

Match at the beginning of a string (or line)

$

Match at the end of a string (or line)

\b

Match at a ‘word’ boundary

\B

Match at not a ‘word’ boundary

These are also known as zero width assertions.

Quantifiers:

These quantifiers apply to the preceding atom.

 

*

Match 0 or more times

+

Match 1 or more times

?

Match 0 or 1 times

{N}

Match exactly N times

{N,}

Match at least N times

{N,M}

Match at least N but not more than M times

 

By default, quantifiers are “greedy”. They attempt to match as many characters as possible. In order to make them match as few

Characters as possible follow them with a question mark “?”.

 

Character class metacharacters:

^

If the first character of a class, negates that class

-

Unless first or last character of a class, used for a range

 

 

Character class shortcuts:

\d [0-9]

A digit

\D [^0-9]

A non-digit

\s [ \t\n\r\f]

A whitespace char

\S [^ \t\n\r\f]

A non-whitespace char.

\w [a-zA-Z0-9_]

A ‘word’ char

\W [^a-zA-Z0-9_]

A ‘non-word’ char

 

These shortcuts can be used either on their own, or within a character class.

 

 

Metaquote & case translations:

\Q

Quote (de-meta) characters until \E

\U

Uppercase characters until \E

\L

Lowercase characters until \E

 

 

Special variables:

$`

The characters to the left of the match

$&

The characters that matched

$’

The characters to the right of the match

\N

The characters captured by the Nth set of parentheses (if on the match side)

$N

The characters captured by the Nth set of parentheses (if not on the match side)

 

 

Modifiers:

These modifiers apply to the entire pattern

/i

Ignore case

/g

Match globally (all)

/m

Let ^ and $ match next to embedded \n

/s

Let . match \n

/x

Ignore most whitespace and allow comments

/e

Evaluate right hand side of s/// as an expression

All except /e apply to both m// and s/// .

 

Binding operators:

=~

True if the regex matches

!~

True if the regex doesn’t match

 

 


Copyright © 2009 Kunal Saxena Inc. All rights reserved