PCRE – PHP Reference Book Blog https://phpreferencebook.com/ PHP Reference: Beginner to Intermediate PHP5 Sun, 01 Oct 2017 17:21:19 +0000 en-US hourly 1 https://wordpress.org/?v=4.9.13 Regular Expression Syntax (RegEx) Sample https://phpreferencebook.com/samples/regular-expression-syntax/ Thu, 05 Feb 2009 05:03:46 +0000 https://phpreferencebook.com/?p=111 Continue reading Regular Expression Syntax (RegEx) Sample]]> The handy Regular Expression Syntax from the PHP book (pages 149-150). Check out the free PDF if you don’t have it already!

Regular Expression Syntax

^ – Start of string
$ – End of string
. – Any single character
( ) – Group of expressions
[] – Item range ( e.g. [afg] means a, f, or g )
[^] – Items not in range ( e.g. [^cde] means not c, d, or e )
– (dash) – character range within an item range ( e.g. [a-z] means a through z )
| (pipe) – Logical or ( e.g. (a|b) means a or b )
? – Zero or one of preceding character/item range
* – Zero or more of preceding character/item range
+ – One or more of preceding character/item range
{integer} – Exactly integer of preceding character/item range ( e.g. a{2} )
{integer,} – Integer or more of preceding character/item range ( e.g. a{2,} )
{integer,integer} – From integer to integer (e.g. a{2,4} means 2 to four of a )
\ – Escape character
[:punct:] – Any punctuation
[:space:] – Any space character
[:blank:] – Any space or tab
[:digit:] – Any digit: 0 through 9
[:alpha:] – All letters: a-z and A-Z
[:alnum:] – All digits and letters: 0-9, a-z, and A-Z
[:xdigit:] – Hexadecimal digit
[:print:] – Any printable character
[:upper:] – All uppercase letters: A-Z
[:lower:] – All lowercase letters: a-z

PERL Compatible (PCRE) only ( preg_*() )

/ – delimiter before and after the expression

Character classes:

\c – Control character
\s – Whitespace
\S – Not whitespace
\d – Digit (0-9)
\D – Not a digit
\w – Letter, Digit, Underscore [a-zA-Z0-9_]
\W – Not a letter
\x – Hexadecimal digit
\O – Octal digit

Modifiers:

i – Case-insensitive
s – Period matches newline
m – ^ and $ match lines
U – Ungreedy matching
e – Evaluate replacement
x – Pattern over several lines

]]>
RegEx PERL Compatible Character Class \w https://phpreferencebook.com/corrections/backslash-w/ Sat, 31 May 2008 02:11:43 +0000 https://phpreferencebook.com/?p=13 (pg. 150)

Book version:

\w – Letter (a-z, A-Z)

Correct version:

\w – Letter, Digit, Underscore [a-zA-Z0-9_]

This is similar to the syntax for alphanumeric: [:alnum:]

]]>