regex – cheat2

http://www.phpro.org/tutorials/Introduction-to-PHP-Regex.html


// the string to match against
$string = 'The cat sat on the matthew';

// matches the letter "a" followed by zero or more "t" characters
echo preg_match("/at*/", $string);

// matches the letter "a" followed by a "t" character that may or may not be present
echo preg_match("/at?/", $string);

// matches the letter "a" followed by one or more "t" characters
echo preg_match("/at+/", $string);

// matches a possible letter "e" followed by one of more "w" characters anchored to the end of the string
echo preg_match("/e?w+\z/", $string);

// matches the letter "a" followed by exactly two "t" characters
echo preg_match("/at{2}/", $string);

// matches a possible letter "e" followed by exactly two "t" characters
echo preg_match("/e?t{2}/", $string);

// matches a possible letter "a" followed by exactly 2 to 6 "t" chars (att attt atttttt)
echo preg_match("/at{2,6}/", $string);

Special Sequences

  • \w – Any “word” character (a-z 0-9 _)
  • \W – Any non “word” character
  • \s – Whitespace (space, tab CRLF)
  • \S – Any non whitepsace character
  • \d – Digits (0-9)
  • \D – Any non digit character
  • . – (Period) – Any character except newline

Meta Characters

  • ^ – Start of subject (or line in multiline mode)
  • $ – End of subject (or line in multiline mode)
  • [ – Start character class definition
  • ] – End character class definition
  • | – Alternates, eg (a|b) matches a or b
  • ( – Start subpattern
  • ) – End subpattern
  • \ – Escape character

Quantifiers

  • n* – Zero or more of n
  • n+ – One or more of n
  • n? – Zero or one occurrences of n
  • {n} – n occurrences exactly
  • {n,} – At least n occurrences
  • {n,m} – Between n and m occurrences (inclusive)

Pattern Modifiers

  • i – Case Insensitive
  • m – Multiline mode – ^ and $ match start and end of lines
  • s – Dotall – . class includes newline
  • x – Extended– comments and whitespace
  • e – preg_replace only – enables evaluation of replacement as PHP code
  • S – Extra analysis of pattern
  • U – Pattern is ungreedy
  • u – Pattern is treated as UTF-8

Point based assertions

  • \b – Word boundary
  • \B – Not a word boundary
  • \A – Start of subject
  • \Z – End of subject or newline at end
  • \z – End of subject
  • \G – First matching position in subject

Assertions

  • (?=) – Positive look ahead assertion foo(?=bar) matches foo when followed by bar
  • (?!) – Negative look ahead assertion foo(?!bar) matches foo when not followed by bar
  • (?<=) – Positive look behind assertion (?<=foo)bar matches bar when preceded by foo
  • (?<!) – Negative look behind assertion (?<!foo)bar matches bar when not preceded by foo
  • (?>) – Once-only subpatterns (?>\d+)bar Performance enhancing when bar not present
  • (?(x)) – Conditional subpatterns
  • (?(3)foo|fu)bar – Matches foo if 3rd subpattern has matched, fu if not
  • (?#) – Comment (?# Pattern does x y or z)
Pattern Usage
>foo The string “foo”
^foo “foo” at the start of a string
foo$ “foo” at the end of a string
^foo$ “foo” when it is alone on a string
[abc] a, b, or c
[a-z] Any lowercase letter
[^A-Z] Any character that is not a uppercase letter
(gif|jpg) Matches either “gif” or “jpeg”
[a-z]+ One or more lowercase letters
[0-9\.\- ] Аny number, dot, or minus sign
^[a-zA-Z0-9_]{1,}$ Any word of at least one letter, number or _
([wx])([yz]) wy, wz, xy, or xz
[^A-Za-z0-9] Any symbol (not a number or a letter)
([A-Z]{3}|[0-9]{4}) Matches three letters or four numbers

Published by

G3n1k

just to remember what i had known :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s