7
MATCHING WITH REGULAR EXPRESSIONS

Matching with Regular Expressions

Embed Size (px)

Citation preview

Page 1: Matching with Regular Expressions

MATCHING WITH REGULAR EXPRESSIONS

Page 2: Matching with Regular Expressions

PATTERN MATCH OPERATORS• The default pattern match operators are / /.

• However, sometimes there are many symbols that appear in your search patterns that can begin to appear confusing.– Ex. /http:\/\// /s+\./

• To alleviate this confusion Perl allows the scripter to designate their own pattern match operators, by using the character m followed by whatever operators they like.– Code: m//

– Ex.m/Godzilla/ m(Mothra) m%Rodan% m@Kong@

Page 3: Matching with Regular Expressions

MATCH MODIFIERS

• Case insensitivity

• Matches any character except a newline

• Ex. /D.g/i

• Matches any character including special symbols and white space

• Ex. m-dude.-s

• ignore whitespace in pattern

• Need to use \t, \s, \s* or \s+

• Ex. m#(.) . /1#x

• /D . g/ix

• m-dude.-is

• m#(.) . /1#isx

• m^dude\.?^sx

si x Combine Flags Ex.

• There are several modifier letters, called flags, which can append a match operator.

Page 4: Matching with Regular Expressions

ANCHORING YOUR SEARCHES

• Matches absolute beginning

• Ex. m{\Ahttps?://}i

• Matches absolute end

• z – no end of line character

• Z – matches end of line character

• Ex. m-.png\z-

• Exact word match

• Not a part of

• Ex. /\bhunt/

• /\bfred\b/

• Nonword-boundary

• Opposite of /b

• Ex. /\bsearch\B/

\z,\Z\A \b \B

• Thus far our search criteria analyze any part of the string for the pattern.

• However there are ways to anchor our searches to the beginning of a line, word, or even the end of them.

Page 5: Matching with Regular Expressions

USING MATCH VARIABLES• When a capture group is used to match part of a pattern the

result is stored in a corresponding variable.

• These values go hand in hand with the capture group. The capture groups are a way to reuse a sequence to match and the match variables is the returned value itself.– Match variables are used for printing the matched criteria.– ***Note: Saves last recalled value so use conditional programming***• Code: $refGrp#

• Ex. /(.)\1/ m*(la)(da)+\2*

$1 $1 $2

Page 6: Matching with Regular Expressions

MORE QUANTIFIERS• Question: What is a quantifier again?

– So far we have worked with *, + and ?

• However with these quantifiers we can only manipulate 0 or more, 1 or more or 1 or less. What happens when we want to only include 5 occurences?– Code: {min, max}

– Ex. /agent(0){2}7/ /agent(0){2, }7/ /agent(0){2, 5}7/

Page 7: Matching with Regular Expressions

REGULAR EXPRESSION PRECEDENCE• The Order of operations of RegEx expressions:

1. Parentheses, Grouping or Capturing: (…), (?:…), (?<LABEL>…)2. Quantifiers: *, +, ?, { , }3. Anchors and Sequence: \A, \b, \z, \Z, ^, $4. Alternatives: |5. Atoms: a, [abc], \1, \g{2}