Search…

Regular Expression Cheat Sheet

08/09/20211 min read
A Regular Expression cheat sheet (Regex cheat sheet) help you quick approach Regex.

Characters

\d 1 digit from 0 to 9
\D 1 character that is not digit
\w Letter, digit or underscore
\W Not letter, digit or underscore
\s Space, \t, \r, \n
\S Not space, \t, \r, \n
. Any characters except line break

Quantifiers

? None or once times
* Zero or more times
+ Once or more times
{n, m} n times to m times
{n, } n times to more times
{n} Exactly n times
\d{2,6} matches 358 in ID is 358

Character classes

[...] One of characters
[x-y] One of characters in range from x to y
[^x] One of charactes that is not x
[^x-y] One of characters that is not in range from x to y
[\d\D] One of characters that is digit or not digit
[AOK] matches A, K in ATTACK
[a-zA-Z] matches a to z, A to Z in text
[3-9] matches 5, 6 in 562 dollars

Special characters

\n New line
\r Carriage return
\t Tab
\OCTAL Octal character OCTAL
\xHEX Hexadecimal character HEX
\xA9S matches ©S in ©STDIO Training

Anchors

^ Start of string or start of line in multiline pattern
\A Start of string
$ End of string or end of line in multiline pattern
\Z End of string
\b Word boundary
\B Not word boundary
.*php$ matches account.php in file path .../account.php but does not match .../account.php.html
\bcoin matches coin in 12 coins but does not match coin in bitcoin

Groups & Logic

xx|yy xx or yy
(...) Capturing group
(?:...) Non-capturing group
\digits Content of group \1, \2, \3...
http:|ftp: matches ftp: in ftp://localhost or http: in http://localhost

Lookarrounds

(?=...) Lookahead
(?!...) Negative lookahead
(?<=...) Lookbehind
(?<!...) Negative lookbehind
(?<!http:)\/\/ matches // that has no https: before in https://training.stdio.vn

Modifiers

g Global match
i Case-insensitive
m Multiple lines
s Treat string as single line
u Treat pattern as Unicode

Meta-characters

(?<!https:)\/\/ matches // that has no https: before in https://localhost//training
Using escape character \ in common

*Regular Expression Engine (REE) process Regular Expression. Different REEs are not fully compatible with each other.

IO Stream

IO Stream Co., Ltd

30 Trinh Dinh Thao, Hoa Thanh ward, Tan Phu district, Ho Chi Minh city, Vietnam
+84 28 22 00 11 12
developer@iostream.co

383/1 Quang Trung, ward 10, Go Vap district, Ho Chi Minh city
Business license number: 0311563559 issued by the Department of Planning and Investment of Ho Chi Minh City on February 23, 2012

©IO Stream, 2013 - 2024