Programação, PHP, Javascript, HTML, CSS, Python
sexta, 3 de setembro

Guia de Expressões Regulares Compatíveis com Perl – PCRE

Funções

preg_match(pattern, subject[, submatches])
preg_match_all(pattern, subject[, submatches])
preg_replace(pattern, replacement, subject)
preg_replace_callback(pattern, callback, subject)
preg_grep(pattern, array)
preg_split(pattern, subject)
Meta Characteres

^ 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 pattern
) End subpattern
\ Escape character
Pattern Modifiers

i Caseless – ignore case
m Multiline mode – ^ and $ match start and end of lines
s Dotall – . class includes newline
x Extended – comments & 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
Base Character Classes

\w Any “word” character (a-z 0-9 _)
\W Any non “word” character
\s Whitespace (space, tab CRLF)
\S Any non whitespace character
\d Digits (0-9)
\D Any non digit character
. (Period) – Any character except newline
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
{,m} At most m occurrences
{n,m} Between n and m occurrences (inclusive)
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
Subpattern Modifiers & Assertions

(?:) Non capturing subpattern ((?:foo|fu)bar) matches foobar or fubar without foo or fu appearing as a captured subpattern
(?=) 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
(?<=) Positiove look behind assertion (?<=foo)bar matches bar when proceded by foo
(?<!) Negative look behind (?<!foo)bar matches bar when not proceded by foo
(?>) Once-only subpatterns (?>d+)bar Performance enhacing when bar not present
(?(x)) Condition subpatterns (?(3)foo|fu)bar Matches foo if 3rd subpattern has matches, fu if not
(?#) Comment (?# Pattern does x y or z)

Referências

Leave a Comment

Sobre o autor

Alejandro Moraga é um cientista da computação. Atualmente é o Webmaster da FAPESP. Já foi o desenvolvedor de sistemas ERP e CRM na Internetmídia. Moraga também desenvolve frameworks, ferramentas, sistemas e sites em nome da empresa METS no qual é sócio junto com sua esposa, e é um pesquisador da Inteligência Artificial.

Mais artigos

Validar números de telefones com expressão regular em PHP
Validar números de telefones com expressão regular em PHP

Tudo que segue um padrão pode ser validado ou tratado por expressões regulares, inclusive os números de [Leia mais]

Caçando e encontrando palavras com expressões regulares
Caçando e encontrando palavras com expressões regulares

As palavras são formadas por letras, que podem ser maiúsculas e minúsculas. Há palavras com acentuação e palavras [Leia mais]

Expressões Regulares – Padrões comuns
Expressões Regulares – Padrões comuns

Expressões regulares (ou regex, abreviação de regular expression) são uma forma compacta de especificar padrões de [Leia mais]