regular expression in python

#Python #biểu thức #REGEX #Programming #data Khoa học ### Biểu thức chính quy trong Python

Biểu thức chính quy là một công cụ mạnh mẽ để khớp mẫu trong văn bản.Chúng có thể được sử dụng để tìm chuỗi các ký tự cụ thể, thay thế văn bản và trích xuất thông tin từ các chuỗi.Trong hướng dẫn này, chúng tôi sẽ học cách sử dụng các biểu thức thông thường trong Python.

Chúng tôi sẽ bắt đầu bằng cách tìm hiểu những điều cơ bản của các biểu thức thông thường, và sau đó chúng tôi sẽ xem cách sử dụng chúng để giải quyết một số vấn đề phổ biến.Đến cuối hướng dẫn này, bạn sẽ hiểu rõ về cách sử dụng các biểu thức thông thường trong Python và bạn sẽ có thể sử dụng chúng để giải quyết các vấn đề của riêng bạn.

#### Những điều cơ bản về biểu thức thông thường

Một biểu thức chính quy là một chuỗi các ký tự xác định một mẫu.Mẫu có thể được sử dụng để phù hợp với một chuỗi các ký tự cụ thể hoặc để phù hợp với bất kỳ chuỗi nào phù hợp với mẫu.

Biểu thức chính quy được tạo thành từ một loạt các ký tự, mỗi ký tự có một ý nghĩa cụ thể.Các ký tự phổ biến nhất là:

*** \ w ** phù hợp với bất kỳ chữ cái, chữ số hoặc dấu gạch dưới nào
*** \ d ** khớp với bất kỳ chữ số nào
*** \ S ** khớp với bất kỳ ký tự khoảng trắng nào (không gian, tab, newline, v.v.)
***. ** Phù hợp với bất kỳ ký tự nào
***^** khớp với sự khởi đầu của chuỗi
*** $ ** khớp với phần cuối của chuỗi

Chúng ta có thể sử dụng các ký tự này để tạo các biểu thức chính quy phù hợp với các chuỗi ký tự cụ thể.Ví dụ: biểu thức chính quy `\ d \ d \ d- \ d \ d \ d- \ d \ d \ d \ d` phù hợp với bất kỳ số điện thoại 10 chữ số nào.

#### Sử dụng các biểu thức thông thường trong Python

Các biểu thức thông thường có thể được sử dụng trong Python với mô -đun `re`.Mô -đun `re` cung cấp một số chức năng để làm việc với các biểu thức chính quy, bao gồm` re.search () `,` re.match () `và` re.findall () `.

Hàm `re.Search ()` Tìm kiếm cho một trận đấu cho một biểu thức chính quy trong một chuỗi.Hàm `re.match ()` Kiểm tra xem phần đầu của chuỗi có khớp với biểu thức chính quy không.Hàm `re.findall ()` Tìm tất cả các kết quả phù hợp cho một biểu thức chính quy trong một chuỗi.

Chúng ta có thể sử dụng các chức năng này để giải quyết một loạt các vấn đề.Ví dụ: chúng ta có thể sử dụng `re.search ()` để tìm địa chỉ email trong một chuỗi hoặc chúng ta có thể sử dụng `re.findall ()` để tìm tất cả các số điện thoại trong tài liệu.

#### Biểu thức thường xuyên trong hành động

Hãy xem làm thế nào chúng ta có thể sử dụng các biểu thức thông thường để giải quyết một số vấn đề phổ biến.

** Tìm địa chỉ email trong một chuỗi **

Chúng ta có thể sử dụng hàm `re.search ()` để tìm địa chỉ email trong một chuỗi.Ví dụ: mã sau tìm thấy địa chỉ email trong chuỗi `" John Doe <[email protected]> "`::

`` `Python
Nhập RE

Email_Regex = R "[A-ZA-Z0-9 _.+-]+@[A-ZA-Z0-9-]+\. [

Match = re.search (email_regex, "John Doe <[email protected]>")

Nếu khớp:
in (match.group ()))
`` `

Mã này sẽ in đầu ra sau:

`` `
[email protected]
`` `

** Tìm tất cả các số điện thoại trong tài liệu **

Chúng ta có thể sử dụng hàm `re.findall ()` để tìm tất cả các số điện thoại trong tài liệu.Ví dụ: mã sau tìm thấy tất cả các số điện thoại trong tệp `" điện thoại_numbers.txt "`:

`` `Python
Nhập RE

Với Open ("Phone_numbers.txt", "R") là f:
Phone_numbers = re.findall (r "\ d \ d \ d- \ d \ d \ d- \ d \ d \ d \ d", f.Read ())

cho điện thoại_number trong điện thoại_numbers:
In (Phone_Number)
`` `

Mã này sẽ in đầu ra sau:

`` `
123-456-7890
234-567-8
=======================================
#Python #Regular Expression #REGEX #Programming #data Science ### Regular Expression in Python

Regular expressions are a powerful tool for pattern matching in text. They can be used to find specific strings of characters, replace text, and extract information from strings. In this tutorial, we will learn how to use regular expressions in Python.

We will start by learning the basics of regular expressions, and then we will see how to use them to solve some common problems. By the end of this tutorial, you will have a good understanding of how to use regular expressions in Python and you will be able to use them to solve your own problems.

#### Basics of Regular Expressions

A regular expression is a sequence of characters that defines a pattern. The pattern can be used to match a specific string of characters, or to match any string that matches the pattern.

Regular expressions are made up of a variety of characters, each of which has a specific meaning. The most common characters are:

* **\w** matches any letter, digit, or underscore
* **\d** matches any digit
* **\s** matches any whitespace character (space, tab, newline, etc.)
* **.** matches any character
* **^** matches the beginning of a string
* **$** matches the end of a string

We can use these characters to create regular expressions that match specific strings of characters. For example, the regular expression `\d\d\d-\d\d\d-\d\d\d\d` matches any 10-digit phone number.

#### Using Regular Expressions in Python

Regular expressions can be used in Python with the `re` module. The `re` module provides a number of functions for working with regular expressions, including `re.search()`, `re.match()`, and `re.findall()`.

The `re.search()` function searches for a match for a regular expression in a string. The `re.match()` function checks if the beginning of a string matches a regular expression. The `re.findall()` function finds all the matches for a regular expression in a string.

We can use these functions to solve a variety of problems. For example, we can use `re.search()` to find the email address in a string, or we can use `re.findall()` to find all the phone numbers in a document.

#### Regular Expressions in Action

Let's see how we can use regular expressions to solve some common problems.

**Find the email address in a string**

We can use the `re.search()` function to find the email address in a string. For example, the following code finds the email address in the string `"John Doe <[email protected]>"`:

```python
import re

email_regex = r"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+"

match = re.search(email_regex, "John Doe <[email protected]>")

if match:
print(match.group())
```

This code will print the following output:

```
[email protected]
```

**Find all the phone numbers in a document**

We can use the `re.findall()` function to find all the phone numbers in a document. For example, the following code finds all the phone numbers in the file `"phone_numbers.txt"`:

```python
import re

with open("phone_numbers.txt", "r") as f:
phone_numbers = re.findall(r"\d\d\d-\d\d\d-\d\d\d\d", f.read())

for phone_number in phone_numbers:
print(phone_number)
```

This code will print the following output:

```
123-456-7890
234-567-8
 
`` `Python
Nhập RE

# Viết biểu thức thông thường phù hợp với số điện thoại ở định dạng
# (xxx) xxx-xxxx.

Phone_Regex = R '^\ (? \ D {3} \)? \ S? \ D {3}-\ d {4} $'
`` `
 
Join ToolsKiemTrieuDoGroup
Back
Top
AdBlock Detected

We get it, advertisements are annoying!

Sure, ad-blocking software does a great job at blocking ads, but it also blocks useful features of our website. For the best site experience please disable your AdBlocker.

I've Disabled AdBlock