2021-06-06 · 6

Regular Expressions Cheatsheet

regextil

This post is over 2 years old. The content may be outdated.

What is a regular expression


What is a regular expression?

A regular expression is, by dictionary definition, a formal language used to express a set of strings following certain rules. It's mainly used in programming languages and text editors for searching and replacing strings. As simple as the code is, this regex has very poor readability, so it has the problem of being hard to understand unless you memorize it or look it up.

Regular expression terms

The symbols used in a regular expression are called Meta characters.

<table style="border-collapse: collapse; width: 100%; height: 280px;" border="1" data-ke-align="alignLeft"><tbody><tr style="height: 20px;"><td style="width: 30%; text-align: center; height: 20px;"><b>Expression</b></td><td style="width: 70%; text-align: center; height: 20px;"><b>Meaning</b></td></tr><tr style="height: 20px;"><td style="width: 30%; height: 20px; text-align: center;">^x</td><td style="width: 70%; text-align: justify; height: 20px;">Represents the start of the string; means it starts with the character x.</td></tr><tr style="height: 20px;"><td style="width: 30%; height: 20px; text-align: center;">x$</td><td style="width: 70%; text-align: justify; height: 20px;">Represents the end of the string; means it ends with the character x.</td></tr><tr style="height: 20px;"><td style="width: 30%; height: 20px; text-align: center;"> .x</td><td style="width: 70%; text-align: justify; height: 20px;">Represents a single arbitrary character position; means the string ends with x.</td></tr><tr style="height: 20px;"><td style="width: 30%; height: 20px; text-align: center;">x+</td><td style="width: 70%; text-align: justify; height: 20px;">Represents repetition; means the character x repeats one or more times.</td></tr><tr style="height: 20px;"><td style="width: 30%; height: 20px; text-align: center;">x?</td><td style="width: 70%; text-align: justify; height: 20px;">Represents existence; means the character x may or may not exist.</td></tr><tr style="height: 20px;"><td style="width: 30%; height: 20px; text-align: center;"> x*</td><td style="width: 70%; text-align: justify; height: 20px;">Represents repetition; means the character x repeats 0 or more times.</td></tr><tr style="height: 20px;"><td style="width: 30%; height: 20px; text-align: center;"> x|y</td><td style="width: 70%; text-align: justify; height: 20px;">Represents or; means the character x or y exists.</td></tr><tr style="height: 20px;"><td style="width: 30%; height: 20px; text-align: center;"> (x)</td><td style="width: 70%; text-align: justify; height: 20px;">Represents a group; means x is handled as a group.</td></tr><tr style="height: 20px;"><td style="width: 30%; height: 20px; text-align: center;"> (x)(y)</td><td style="width: 70%; text-align: justify; height: 20px;">Represents a set of groups; numbers are assigned in order from the front for management, and x, y are managed as each group's data.</td></tr><tr style="height: 20px;"><td style="width: 30%; height: 20px; text-align: center;">(x)(?:y)</td><td style="width: 70%; text-align: justify; height: 20px;">Represents an exception to the set of groups; means it is not managed as a group set.</td></tr><tr style="height: 20px;"><td style="width: 30%; height: 20px; text-align: center;">x{n}</td><td style="width: 70%; text-align: justify; height: 20px;">Represents repetition; means the character x repeats n times.</td></tr><tr style="height: 20px;"><td style="width: 30%; text-align: center; height: 20px;">x{n,}</td><td style="width: 70%; text-align: justify; height: 20px;">Represents repetition; means the character x repeats n or more times.</td></tr><tr style="height: 20px;"><td style="width: 30%; text-align: center; height: 20px;">x{n,m}</td><td style="width: 70%; text-align: justify; height: 20px;">Represents repetition; means the character x repeats at least n and at most m times<span style="color: #000000;">.</span></td></tr></tbody></table>

Among the Meta characters, some are used more specially. [] has the special meaning of selecting only one character from the range of the specified string inside it.

<table style="border-collapse: collapse; width: 100%; height: 300px;" border="1" data-ke-align="alignLeft"><tbody><tr style="height: 20px;"><td style="width: 30.2326%; text-align: center; height: 20px;"><b>Expression</b></td><td style="width: 69.7674%; text-align: center; height: 20px;"><b>Meaning</b></td></tr><tr style="height: 20px;"><td style="width: 30.2326%; text-align: center; height: 20px;">[xy]</td><td style="width: 69.7674%; text-align: justify; height: 20px;">Represents character selection; means one of x and y.</td></tr><tr style="height: 20px;"><td style="width: 30.2326%; text-align: center; height: 20px;"> [^xy]</td><td style="width: 69.7674%; text-align: justify; height: 20px;">Represents not; means a character excluding  x and y.</td></tr><tr style="height: 20px;"><td style="width: 30.2326%; text-align: center; height: 20px;"> [x-z]</td><td style="width: 69.7674%; text-align: justify; height: 20px;">Represents a range; means a character between x and z.</td></tr><tr style="height: 20px;"><td style="width: 30.2326%; text-align: center; height: 20px;">^</td><td style="width: 69.7674%; text-align: justify; height: 20px;">Represents escape; means using ^ as a character.</td></tr><tr style="height: 20px;"><td style="width: 30.2326%; text-align: center; height: 20px;"> \b</td><td style="width: 69.7674%; text-align: justify; height: 20px;">Represents a word boundary; means a character between a character and whitespace.</td></tr><tr style="height: 20px;"><td style="width: 30.2326%; text-align: center; height: 20px;">\B</td><td style="width: 69.7674%; text-align: justify; height: 20px;">Represents a non-word boundary; means a character that is not between a character and whitespace.</td></tr><tr style="height: 20px;"><td style="width: 30.2326%; text-align: center; height: 20px;"> \d</td><td style="width: 69.7674%; text-align: justify; height: 20px;">Represents a digit; means a number.</td></tr><tr style="height: 20px;"><td style="width: 30.2326%; text-align: center; height: 20px;"> \D</td><td style="width: 69.7674%; text-align: justify; height: 20px;">Represents non-digit; means something that is not a number.</td></tr><tr style="height: 20px;"><td style="width: 30.2326%; text-align: center; height: 20px;"> \s</td><td style="width: 69.7674%; text-align: justify; height: 20px;">Represents a space; means a whitespace character.</td></tr><tr style="height: 20px;"><td style="width: 30.2326%; text-align: center; height: 20px;"> \S</td><td style="width: 69.7674%; text-align: justify; height: 20px;">Represents non-space; means something that is not a whitespace character.</td></tr><tr style="height: 20px;"><td style="width: 30.2326%; text-align: center; height: 20px;">\t</td><td style="width: 69.7674%; text-align: justify; height: 20px;">Represents tab; means a tab character.</td></tr><tr style="height: 20px;"><td style="width: 30.2326%; text-align: center; height: 20px;">\v</td><td style="width: 69.7674%; text-align: justify; height: 20px;">Newly finds a tab character. </td></tr><tr style="height: 20px;"><td style="width: 30.2326%; text-align: center; height: 20px;">\w</td><td style="width: 69.7674%; text-align: justify; height: 20px;">Represents a word; means one character among alphabet + digit + _.</td></tr><tr style="height: 20px;"><td style="width: 30.2326%; text-align: center; height: 20px;">\W</td><td style="width: 69.7674%; text-align: justify; height: 20px;">Represents non-word; means a character that is not alphabet + digit + _.</td></tr></tbody></table>

When using regular expressions there's something called a Flag; if you don't use a Flag, it processes the search on the string only once and terminates.

<table style="border-collapse: collapse; width: 100%; height: 80px;" border="1" data-ke-align="alignLeft"><tbody><tr style="height: 20px;"><td style="width: 15.5814%; text-align: center; height: 20px;"><b>Flag</b></td><td style="width: 84.4186%; text-align: center; height: 20px;"><b>Meaning</b></td></tr><tr style="height: 20px;"><td style="width: 15.5814%; text-align: center; height: 20px;"> g</td><td style="width: 84.4186%; text-align: justify; height: 20px;">Represents Global; means searching all patterns within the target string.</td></tr><tr style="height: 20px;"><td style="width: 15.5814%; text-align: center; height: 20px;">i</td><td style="width: 84.4186%; text-align: justify; height: 20px;">Represents Ignore case; means not distinguishing upper/lowercase for the target string.</td></tr><tr style="height: 20px;"><td style="width: 15.5814%; text-align: center; height: 20px;">m</td><td style="width: 84.4186%; text-align: justify; height: 20px;">Represents Multi line; means searching even when the target string is a multi-line string.</td></tr></tbody></table>

Example

/[0-9]/g
Finds only 'one' digit among 0~9 in the whole string
/[no]/g
Finds only 'one' character, either 'n' or 'o', in the whole string
/^[0-9a-zA-Z]([-_\.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-_\.]?[0-9a-zA-Z])*\.[a-zA-Z]{2,3}$/i

Starts with one of a digit 0~9 or an alphabet a~z A~Z, and may contain - _ \ . in the middle,
0~9 a~z A~Z can repeat. Next, @ must come, and one of 0~9 a~z A~Z must exist,
after that - _ \ . may exist and 0~9 a~z A~Z can repeat. Next, . must exist,
and a~z A~Z must exist 2~3 times. This whole regex is case-insensitive.

Reference:

https://hamait.tistory.com/342

https://choseongho93.tistory.com/130


Original (Korean): tistory — published 2021-06-06, migrated to this blog. This translation was generated with the help of AI.

Comments

Delete this comment?

Related posts

Regular Expressions Cheatsheet · 나봄하랑