Docsity

Regular Expressions in PHP: Understanding Patterns and Matching, Study notes of Computer Science

Partial preview of the text

Download Regular Expressions in PHP: Understanding Patterns and Matching and more Study notes Computer Science in PDF only on Docsity!

Regular Expression

PHP Odds and Ends

COMP 416

Fall 2007

Lecture 7

Regular Expressions

• A powerful way of manipulating strings.

• RE expresses a text pattern.

– Pattern can be used to pick out substrings.

– Pattern can be self-referential.

• Used to provide complex matching and

substitution operations.

Specifying an RE

• RE starts with a delimiter.

• Followed by the pattern.

• Ends with a delimiter.

• Traditionally, delimiter is /

– But it can be anything.

The Simplest Pattern

• A sequence of characters matches itself.

• /Hello/

– Matches

  • Hello
  • Hello there
  • Hey, Hello

– Does not match

  • hello
  • h ell o
  • goodbey

• Matches any single character

– Including spaces, tabs, newlines etc.

Quantifiers

• * : match 0 or more times.

• + : match 1 or more times.

•? : match 0 or 1 time.

• {n} : match n times.

• {n,m} : match at least n and up to m times.

• Examples:

• ba*b, ba+b, ba?b, ba{3,5}b

– Try these against: bb, bab, baaab, baaaaaab

Grouping

  • ()’s group portions of the pattern so that quantifiers can be

applied to them.

/H(ell)*o/

/(H(ell)*o){2}/

“Ho”?

“Hellello”?

“Helllo”?

“HelloHello”?

“HoHello”?

Character classes

  • [ begins the definition of a character class.

• A character class specifies a set of possible matches at

that point in the pattern

/He[lo]$/ Matches “Hel” or “Heo”, but NOT “Helo” /He[lo]$/ Matches “Hel”, “Heo”, and “Helo”*

Alternatives

• | specifies alternatives

/Hello|Bye/ Matches “Hello” or “Bye” /(Hello|Bye)+/ Matches “Hello”, “Bye”, “HelloHello”, “ByeBye”, “HelloBye”, and “ByeHello”

m/Hell(o|B)ye/

What if we meant “o or B” after “Hell” and before “ye”?

The Escape

• \ is used to escape a metacharacter

/./

Matches anything with a “.” in it. /\/ Matches anything with a “\” in it. /^|($/ Matches “^|($“ or “hello^|($there”

Character class shortcuts

• A whole bunch of shortcuts for common

character classes and codes for special

characters

\n, \t, \s, \S : newline, tab, whitespace, non-

whitespace

\d, \D : digit, non-digit

\w, \W : word character [a-zA-Z_0-9], non-word

character

Greedy vs. Non-Greedy

• By default, matches attempt to be greedy.

– Consider: /a(.*)a/

  • String: This is a good time to take a break.
  • What matched?

• Non-greedy quantifiers

– Rethink example with: /a(.*?)a/

• If you’re pattern has .*, you probably really

want .*?

Back to preg_match

  • preg_match( pattern , string , [$ matches, [ flags, [ offset ]]])
  • If $matches is provided:
    • Will be set to an array with all of the captured substrings.
    • Entry 0 will be the whole match.
    • 1 will be the first captured substring, 2 will be the second, etc.
  • Flags can be set to PREG_OFFSET_CAPTURE
    • If set, then matches will be 2 dimensional.
    • $matches[0][0] : whole match, $matches[0][1] : offset of match
    • $matches[1][0] : first captured substring, $matches[1][1] : offset
    • etc…
  • If offset is set, then pattern match will begin at that

character position of the target string.

Regular Expression in CS

• Turns out lots of fundamental problems of

information and computing can be cast as

problems of RE matching and substitution.

• RE matching and substitution is hard.

  • Straightforward algorithm is to explore every possible

branch and possibility.

  • Can we do better?
  • What tricks can you play?
  • COMP 455

• Practical implications:

  • Be aware that RE processing can hurt performance.