Regex has something interesting

Tram Ho

Preamble

In projects, authentication of user information is always necessary. There are many ways to validate information, but the simplest is probably still using Regex. In this article I will introduce you to the basics of regex and some commonly used patterns.

1. What is regex

The full regex, Regular Expression (translated into Vietnamese as a regular expression), is a paragraph of special characters used to match strings or a set of strings. It is really useful in many cases such as extracting information from a text (code, file, log, excel file, doc, etc.)

2. Pattern syntaxes in Regex

Surely you more or less have encountered a long code of code similar to the above without understanding what it means? So let’s find out some special characters used in regex to know what it means: 3.

Before introducing the special characters, I shared a tool to test the regex as https://regex101.com/

A backslash turns a lowercase character adjacent to a special character. For example if ‘w’ without the backslash would match the lowercase w characters. But with the forward slash, w , it becomes a special character.
Details of these special characters, you can see more here .

Don’t forget that is also a character that can be matched, so \ can be used to match this character.

^

Matches the leading characters of a string.

For example, /^A/ won’t match an Apple because A is not at the top of the string, but it will match Apple because A at the top.

$

Match the characters at the end of the string.

For example, /g$/ would match the Huấn Hoa Hồng because there is a g character at the end, and it does not match Khá Bảnh because there is no g character at the end.

{n}

The preceding character must appear n times. n must be 1 integer.

For example, /a{2}/ doesn’t match soma but matches somaa

{n, m}

The preceding character must appear from n to m times. n, m must be 1 integer. If m is omitted, it is equivalent to ∞.

For example, /a{2,5}/ does not match somaaaaaa but matches somaaa

*

Allow the preceding character to repeat 0 or more times. The equivalent of writing {0,} For example, /ha*/ does not match hihi but matches haha

+

Allow the preceding character to repeat 1 or more times. Equivalent to writing {1,}.

For example, /a+/ matches a in soma and cx matches a in somaaa

?

Allow the preceding character to repeat 0 times or 1 time only. Equivalent to spelling {0,1}.

For example, /lo?/ Matches lo in viblo and but not in lo in alolo

.

Sign . match any single character except a new line.

For example, /.a/ would not match with alo , but would match ba , ma

[az]

Type this pattern to match any 1 character in square brackets.

The - sign indicates that from a to z, you can also use [abcd] instead of [ad]

You can use the preceding ^ sign to denote negation

For example, [^ abc], the match character will not include a, b, c

Share the news now

Source : Viblo