PcreAndPhp

Match

Functions

  • preg_match : the most important function used 90% of the time
  • preg_quote : used when you want to turn a string into pattern
  • preg_grep : used to filter array

Characters and Character Classes

  • general match: ., [^"]
  • character class: \s, \w, \d, \S
  • chinese character: \p{Han}

Need to enable unicode modifier

  • custom character set: [a-y], [02468]

Group and Repitition

  • Grouping with ( and ), or (?: )
  • Repetition: ?, +, *, {1,5}
  • Non-possessive repetition: ??, +?, *?
  • Or: (?:udomain|onehosting|webhost)

Modifiers

  • ignore case: /xxx/i
  • verbose style: /xxx xx/x

Anchor and Tests

  • boundary : ^, $, \b
  • look-around test: (?! ), (?= ), (?<= ), (?<= )

Extract

Functions

  • preg_match : usually for short string and known occurrence count
  • preg_match_all : for longer string and repetitive captures

Capture

  • unnamed : ( )
  • named : \b(?<year>\d{4})\b

Replace

Functions

  • preg_replace : more precise and powerful than str_replace
  • preg_split : more precise and powerful than explode
  • preg_replace_callback : more fancy string processing

Back reference

  • back reference in pattern : \1 \g{1} \g{name} \k<name>
  • back reference in replacement : $1, ${1}

References