As in the previous article, I introduced some basic regex concepts. In this article I will introduce to you the use of regex in the programming language, here is python.
1. Library
Regex uses the re module to work in python, this module is a built-in library in python, you don’t need to install it to use it.
1 2 | import re |
2. Some functions
2.1. re.match
Syntax
1 2 | re.match(pattern, string, flags=0) |
Match pattern with string with arbitrary flags. Here is the syntax for this function.
1 2 3 4 | pattern : Đây là chuỗn cần so khớp. string : Đây là chuỗi để tìm kiếm pattern cón tồn tại trong đó không. flags : Bạn có thể xác định các flag khác nhau bởi sử dụng toán tử |. Các modifier này sẽ được liệt kê ở bảng bên dưới. |
The re.match function returns 1 object if the match succeeds and returns None if it fails
2.2. re.search
Syntax
1 2 | re.search(pattern, string, flags=0) |
This function does a string search on 1 string and returns the matched values
2.3. re.split
Syntax
1 2 | re.split(pattern, string, maxsplit=0, flags=0) |
re.split returns the characters separated by the pattern