Hướng dẫn what does re sub return in python? - re sub trả lại trong python là gì?

Tóm tắt: Trong hướng dẫn này, bạn sẽ tìm hiểu về hàm Python Regex

import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

Code language: Python (python)
5 trả về một chuỗi sau khi thay thế mẫu phù hợp trong một chuỗi bằng một thay thế.: in this tutorial, you’ll learn about the Python regex

import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

Code language: Python (python)
5 function that returns a string after replacing the matched pattern in a string with a replacement.

Giới thiệu về chức năng phụ Python Regex

import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

Code language: Python (python)
5 là một hàm trong mô-đun

import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

Code language: Python (python)
7 tích hợp xử lý các biểu thức chính quy. Hàm

import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

Code language: Python (python)
5 có cú pháp sau:

re.sub(pattern, repl, string, count=0, flags=0)

Code language: Python (python)

Trong cú pháp này:

  • import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

    Code language: Python (python)
    9 là một biểu thức thông thường mà bạn muốn khớp. Bên cạnh một biểu thức chính quy,

    import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

    Code language: Python (python)
    9 có thể là đối tượng

    2124567890

    Code language: Python (python)
    1.
  • 2124567890

    Code language: Python (python)
    2 là sự thay thế
  • 2124567890

    Code language: Python (python)
    3 là chuỗi đầu vào
  • Tham số

    2124567890

    Code language: Python (python)
    4 Chỉ định số lượng trận đấu tối đa mà hàm

    import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

    Code language: Python (python)
    5 sẽ thay thế. Nếu bạn chuyển số 0 vào tham số

    2124567890

    Code language: Python (python)
    4 hoặc bỏ qua hoàn toàn nó, hàm

    import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

    Code language: Python (python)
    5 sẽ thay thế tất cả các trận đấu.
  • 2124567890

    Code language: Python (python)
    8 là một hoặc nhiều cờ Regex sửa đổi hành vi tiêu chuẩn của mẫu.

Hàm

import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

Code language: Python (python)
5 tìm kiếm mẫu trong chuỗi và thay thế các chuỗi phù hợp với sự thay thế (

2124567890

Code language: Python (python)
2).

Nếu hàm

import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

Code language: Python (python)
5 không thể tìm thấy một trận đấu, nó sẽ trả về chuỗi ban đầu. Mặt khác, hàm

import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

Code language: Python (python)
5 trả về chuỗi sau khi thay thế các trận đấu.

Lưu ý rằng hàm

import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

Code language: Python (python)
5 thay thế các lần xuất hiện không chồng chéo ngoài cùng bên trái của mẫu. Và bạn sẽ thấy nó một cách chi tiết trong ví dụ sau.

Hãy cùng lấy một số ví dụ về việc sử dụng chức năng Regex

import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

Code language: Python (python)
5.

1) Sử dụng chức năng Regex import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)Code language: Python (python)5 để trả về số điện thoại đơn giản

Ví dụ sau sử dụng chức năng

import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

Code language: Python (python)
5 để biến số điện thoại

import re pattern = '00' s = '00000' result = re.sub(pattern,'',s) print(result)

Code language: Python (python)
7 thành

import re pattern = '00' s = '00000' result = re.sub(pattern,'',s) print(result)

Code language: Python (python)
8:

import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

Code language: Python (python)

Output:

2124567890

Code language: Python (python)

Trong ví dụ này,

import re pattern = '00' s = '00000' result = re.sub(pattern,'',s) print(result)

Code language: Python (python)
9 là một bộ ký tự chữ số nghịch đảo phù hợp với bất kỳ ký tự đơn nào không phải là một chữ số. Do đó, hàm

import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

Code language: Python (python)
5 thay thế tất cả các ký tự không chữ số bằng chuỗi trống

0

Code language: Python (python)
1.

2) Sử dụng chức năng Regex import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)Code language: Python (python)5 để thay thế các lần xuất hiện không chồng chéo ngoài cùng bên trái của một mẫu

Ví dụ sau đây thay thế

0

Code language: Python (python)
3 bằng

0

Code language: Python (python)
1 trong chuỗi

0

Code language: Python (python)
5:

import re pattern = '00' s = '00000' result = re.sub(pattern,'',s) print(result)

Code language: Python (python)

Output:

0

Code language: Python (python)

Trong ví dụ này, chúng tôi thay thế hai số không bằng các chuỗi trống. Vì vậy, hai cái đầu tiên được kết hợp và thay thế, sau đó hai số 0 sau đây là các trận đấu và được thay thế quá, và cuối cùng, chữ số cuối cùng không thay đổi.

3) Sử dụng Regex import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)Code language: Python (python)5 với ví dụ

Ví dụ sau đây sử dụng hàm

import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

Code language: Python (python)
5 để thay thế văn bản được bao quanh bằng (

0

Code language: Python (python)
8) (định dạng đánh dấu của nó) bằng thẻ

0

Code language: Python (python)
9 trong HTML:

import re s = 'Make the World a *Better Place*' pattern = r'\*(.*?)\*' replacement = r'\1<\\b>' html = re.sub(pattern, replacement, s) print(html)

Code language: Python (python)

Output:

import re s = 'Make the World a *Better Place*' pattern = r'\*(.*?)\*' replacement = r'\1<\\b>' html = re.sub(pattern, replacement, s) print(html)

Code language: Python (python)

Output:

Make the World a Better Place<\b>

Code language: Python (python)

Trong ví dụ này, mẫu

import re s = 'Make the World a *Better Place*' pattern = r'\*(.*?)\*' replacement = r'\1<\\b>' html = re.sub(pattern, replacement, s) print(html)

Code language: Python (python)
0 tìm văn bản bắt đầu và kết thúc bằng dấu hoa thị (

0

Code language: Python (python)
8). Nó có một nhóm bắt giữ nắm bắt văn bản giữa các dấu sao (

0

Code language: Python (python)
8).

Sự thay thế là một biểu thức thường xuyên với một bản sao lưu. Bản sao lưu

import re s = 'Make the World a *Better Place*' pattern = r'\*(.*?)\*' replacement = r'\1<\\b>' html = re.sub(pattern, replacement, s) print(html)

Code language: Python (python)
3 đề cập đến nhóm đầu tiên trong mẫu, đó là văn bản giữa các dấu sao (

0

Code language: Python (python)
8).

4) Sử dụng hàm regex import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)Code language: Python (python)5 với sự thay thế làm hàm

Giả sử bạn có một danh sách các chuỗi trong đó mỗi phần tử chứa cả bảng chữ cái và số:

l = ['A1','A2','A3']

Code language: Python (python)

Và bạn muốn vuông số trong mỗi phần tử danh sách. Ví dụ, A1 trở thành A1, A2 trở thành A4 và A3 trở thành A9. Để làm điều này, bạn có thể sử dụng chức năng

import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

Code language: Python (python)
5.

Đối số thứ hai của hàm

import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

Code language: Python (python)
5 (

2124567890

Code language: Python (python)
2) có thể là một hàm. Trong trường hợp này, hàm

import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

Code language: Python (python)
5 sẽ gọi chức năng này cho mọi lần xuất hiện không chồng chéo của mẫu.

Hàm này (

2124567890

Code language: Python (python)
2) có một đối số đối tượng

import re s = 'Make the World a *Better Place*' pattern = r'\*(.*?)\*' replacement = r'\1<\\b>' html = re.sub(pattern, replacement, s) print(html)

Code language: Python (python)
1 duy nhất và trả về chuỗi thay thế.

Sau đây minh họa cách sử dụng đối số thứ hai làm hàm:

import re def square(match): num = int(match.group()) return str(num*num) l = ['A1','A2','A3'] pattern = r'\d+' new_l = [re.sub(pattern, square, s) for s in l] print(new_l)

Code language: Python (python)

Output:

import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

Code language: Python (python)
0

Làm thế nào nó hoạt động.

Đầu tiên, xác định danh sách các chuỗi:

l = ['A1','A2','A3']

Code language: Python (python)

Thứ hai, xác định một mẫu

import re s = 'Make the World a *Better Place*' pattern = r'\*(.*?)\*' replacement = r'\1<\\b>' html = re.sub(pattern, replacement, s) print(html)

Code language: Python (python)
2 khớp với một hoặc nhiều chữ số:

import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

Code language: Python (python)
2

Thứ ba, thay thế các chữ số bằng hình vuông của chúng bằng cách gọi hàm

import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

Code language: Python (python)
5 và truyền hàm

import re s = 'Make the World a *Better Place*' pattern = r'\*(.*?)\*' replacement = r'\1<\\b>' html = re.sub(pattern, replacement, s) print(html)

Code language: Python (python)
4:

import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

Code language: Python (python)
3

Cuối cùng, xác định hàm

import re s = 'Make the World a *Better Place*' pattern = r'\*(.*?)\*' replacement = r'\1<\\b>' html = re.sub(pattern, replacement, s) print(html)

Code language: Python (python)
4 bình phương chữ số phù hợp và trả về nó:

import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

Code language: Python (python)
4

Bản tóm tắt

  • Sử dụng chức năng Python Regex

    import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

    Code language: Python (python)
    5 để thay thế các sự xuất hiện của các trận đấu của một mẫu bằng một sự thay thế.

    import re phone_no = '(212)-456-7890' pattern = '\D' result = re.sub(pattern, '',phone_no) print(result)

    Code language: Python (python)
    5
    function to replace the occurrences of matches of a pattern with a replacement.

Bạn có thấy hướng dẫn này hữu ích không?

RE trở lại trong Python là gì?

RE.Search () và Re. Match () Cả hai đều là các hàm của mô -đun RE trong Python. Các chức năng này rất hiệu quả và nhanh chóng để tìm kiếm trong chuỗi. Hàm tìm kiếm một số chuỗi con trong một chuỗi và trả về một đối tượng khớp nếu tìm thấy, nếu không nó sẽ trả về không.a match object if found, else it returns none.

RE SUB có thay thế tất cả không?

Theo mặc định, số lượng được đặt thành 0, có nghĩa là phương thức re.sub () sẽ thay thế tất cả các lần xuất hiện mẫu trong chuỗi đích.the re. sub() method will replace all pattern occurrences in the target string.

Reat () return là gì?

khớp () hàm.Khi được cung cấp một biểu thức thông thường, Re.hàm match () kiểm tra chuỗi được khớp cho một mẫu trong regex và trả về lần xuất hiện đầu tiên của một mẫu phù hợp như vậy.Hàm này chỉ kiểm tra một trận đấu ở đầu chuỗi.the first occurrence of such a pattern match. This function only checks for a match at the beginning of the string.

Sub Regex hoạt động như thế nào?

Hàm Sub () tìm kiếm mẫu trong chuỗi và thay thế các chuỗi phù hợp bằng thay thế (repl).Nếu hàm sub () không thể tìm thấy một trận đấu, nó sẽ trả về chuỗi gốc.Mặt khác, hàm sub () trả về chuỗi sau khi thay thế các trận đấu.searches for the pattern in the string and replaces the matched strings with the replacement ( repl ). If the sub() function couldn't find a match, it returns the original string. Otherwise, the sub() function returns the string after replacing the matches.