http://qs321.pair.com?node_id=717987

Dereferencing Syntax


Direct

Using References

Syntax 1

Syntax 2

array

element

$a[0]

${$ar}[0]

$ar->[0]

slice

@a [0,1,2]

@{$ar}[0,1,2]

unindexed

@a

@{$ar}

last index

$#a

$#{$ar}

hash

element

$h{'a'}

${$hr}{'a'}

$hr->{'a'}

slice

@h{'a', 'b', 'c'}

@{$hr}{'a', 'b', 'c'}

unindexed

%h

%{$hr}

code

call

func(@args)

&{$cr}(@args)

$cr->(@args)


* – The curly brackets around $ar, $hr and $cr are optional.

I got this from the scratchpad of ikegami which I think is really useful.

Regex Summary

Metacharacter

Meaning

[abc]

Any one of a, b, or c.

[abc]

Anything other than a, b, and c.

\d \D

A digit; a non-digit.

\w \W

A 'word' character; a non-'word' character.

\s \S

A whitespace character; a non-whitespace character.

\b

The boundary between a \w character and a \W character

.

Any single character (apart from a new line).

(abc)

The phrase 'abc' as a group.

?

Preceding character or group may be present 0 or 1 times.

+

Preceding character or group is present 1 or more times.

*

Preceding character or group may be present 0 or more times.

{x,y}

Preceding character or group is present between x and y times.

{,y}

Preceding character or group is present at most y times.

{x,}

Preceding character or group is present at least x times.

{x}

Preceding character or group is present x times.

Non-greediness For Quantifiers

Syntax: (pattern)+?

(pattern)*?

The metacharacters + and * are greedy by default and will try to match as much as possible of the referenced string (while still achieving a full pattern match). This 'greedy' behavior can be turned off by placing a ? immediately after the respective metacharacter. A non-greedy match finds the minimum number of characters matching the pattern.

Grouping and Alternation

| For Alternation

Syntax: pattern1|pattern2

By separating two patterns with |, we can specify that a match on one or the other should be attempted.

() For Grouping And Backreferences ('Capturing')

Syntax: (pattern)

This will group elements in pattern. If those elements are matched, a backreference is made to one of the numeric special variables ($1, $2, $3 etc.)

 (?:) For Non-backreferenced Grouping ('Clustering')

Syntax: (?:pattern)

This will group elements in pattern without making backreferences.

Lookahead/behind Assertions

 (?=) For Positive Lookahead

Syntax: pattern1(?=pattern2)

This lets us look for a match on 'pattern1 followed by pattern2', without backreferencing pattern2.

(?!) For Negative Lookahead

Syntax: pattern1(?!pattern2)

This lets us look for a match on 'pattern1 not followed by pattern2', without backreferencing pattern2.

(?<=) For Positive Lookbehind

Syntax: pattern1(?<=pattern2)

This lets us look for a match on 'pattern1 preceded by pattern2', without backreferencing pattern2. This only works if pattern2 is of fixed width.

(?<!) For Negative Lookbehind

Syntax: pattern1(?<!pattern2)

This lets us look for a match on 'pattern1 not preceded by pattern2', without backreferencing pattern2. This only works if pattern2 is of fixed width.

Backreference Variables

Variable

Description

\num (num = 1, 2, 3…)

Within a regular expression, \num returns the substring that was matched with the numth grouped pattern in that regexp.

$num (num = 1, 2, 3…)

Outside a regular expression, $num returns the substring that was matched with the numth grouped pattern in that regexp.

$+

This returns the substring matched with the last grouped pattern in a regexp.

$&

This returns the string that matched the whole regexp – this will include portions of the string that matched (?:) groups, which are otherwise not backreferenced.

$`

This returns everything preceding the matched string in $&.

$'

This returns everything following the matched string in $&.