Hướng dẫn list and explain the option flags used in regular expression modifiers in python - liệt kê và giải thích các cờ tùy chọn được sử dụng trong công cụ sửa đổi biểu thức chính quy trong python

Python Regex cho phép các cờ tùy chọn chỉ định khi sử dụng các mẫu biểu thức chính quy với match(), search()split(), trong số các mẫu khác.

Show

Tất cả các phương thức mô -đun RE đều chấp nhận một đối số cờ tùy chọn cho phép các tính năng và biến thể cú pháp độc đáo khác nhau.

Ví dụ: bạn muốn tìm kiếm một từ bên trong một chuỗi bằng regex. Bạn có thể nâng cao khả năng Regex này bằng cách thêm cờ RE.I làm đối số vào phương thức tìm kiếm để cho phép tìm kiếm không nhạy cảm trường hợp.RE.I flag as an argument to the search method to enable case-insensitive searching.

Bạn sẽ tìm hiểu cách sử dụng tất cả các cờ Regex có sẵn trong Python với các ví dụ ngắn và rõ ràng.

Đầu tiên, tham khảo bảng dưới đây để biết cờ Regex có sẵn.refer to the below table for available regex flags.

Lá cờCú pháp dàiNghĩa
import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']
0
import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']
1
Thực hiện kết hợp ASCII-chỉ thay vì khớp unicode đầy đủ
import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']
2
import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']
3
Thực hiện kết hợp không nhạy cảm trường hợp
import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']
4
import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']
5
Cờ này được sử dụng với Metacharacter
import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']
6 (CARET) và
import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']
7 (Dollar). Khi cờ này được chỉ định, Metacharacter
import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']
6 phù hợp với mô hình khi bắt đầu chuỗi và mỗi lần bắt đầu của Newline (____ 19). Và Metacharacter
import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']
7 của chuỗi và kết thúc của mỗi dòng mới (
import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']
9)
When this flag is specified, the metacharacter
import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']
6 matches the pattern at beginning of the string and each newline’s beginning (
import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']
9).
And the metacharacter
import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']
7 matches pattern at the end of the string and the end of each new line (
import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']
9)
import re

# string with newline character
target_str = "ML\nand AI"

# Match any character
result = re.search(r".+", target_str)
print("Without using re.S flag:", result.group())
# Output 'ML'

# With re.S flag
result = re.search(r".+", target_str, re.S)
print("With re.S flag:", result.group())
# Output 'ML\nand AI'

# With re.DOTALL flag
result = re.search(r".+", target_str, re.DOTALL)
print("With re.DOTALL flag:", result.group())
# Output 'ML\nand AI'
2
import re

# string with newline character
target_str = "ML\nand AI"

# Match any character
result = re.search(r".+", target_str)
print("Without using re.S flag:", result.group())
# Output 'ML'

# With re.S flag
result = re.search(r".+", target_str, re.S)
print("With re.S flag:", result.group())
# Output 'ML\nand AI'

# With re.DOTALL flag
result = re.search(r".+", target_str, re.DOTALL)
print("With re.DOTALL flag:", result.group())
# Output 'ML\nand AI'
3
Làm cho ký tự đặc biệt DOT (
import re

# string with newline character
target_str = "ML\nand AI"

# Match any character
result = re.search(r".+", target_str)
print("Without using re.S flag:", result.group())
# Output 'ML'

# With re.S flag
result = re.search(r".+", target_str, re.S)
print("With re.S flag:", result.group())
# Output 'ML\nand AI'

# With re.DOTALL flag
result = re.search(r".+", target_str, re.DOTALL)
print("With re.DOTALL flag:", result.group())
# Output 'ML\nand AI'
4) khớp với bất kỳ ký tự nào, bao gồm cả dòng mới. Không có cờ này, DOT (____ 24) sẽ khớp với bất cứ điều gì ngoại trừ một dòng mới
import re

# string with newline character
target_str = "ML\nand AI"

# Match any character
result = re.search(r".+", target_str)
print("Without using re.S flag:", result.group())
# Output 'ML'

# With re.S flag
result = re.search(r".+", target_str, re.S)
print("With re.S flag:", result.group())
# Output 'ML\nand AI'

# With re.DOTALL flag
result = re.search(r".+", target_str, re.DOTALL)
print("With re.DOTALL flag:", result.group())
# Output 'ML\nand AI'
6
import re

# string with newline character
target_str = "ML\nand AI"

# Match any character
result = re.search(r".+", target_str)
print("Without using re.S flag:", result.group())
# Output 'ML'

# With re.S flag
result = re.search(r".+", target_str, re.S)
print("With re.S flag:", result.group())
# Output 'ML\nand AI'

# With re.DOTALL flag
result = re.search(r".+", target_str, re.DOTALL)
print("With re.DOTALL flag:", result.group())
# Output 'ML\nand AI'
7
Cho phép bình luận trong Regex. Cờ này rất hữu ích để làm cho Regex dễ đọc hơn bằng cách cho phép nhận xét trong Regex.
import re

# string with newline character
target_str = "ML\nand AI"

# Match any character
result = re.search(r".+", target_str)
print("Without using re.S flag:", result.group())
# Output 'ML'

# With re.S flag
result = re.search(r".+", target_str, re.S)
print("With re.S flag:", result.group())
# Output 'ML\nand AI'

# With re.DOTALL flag
result = re.search(r".+", target_str, re.DOTALL)
print("With re.DOTALL flag:", result.group())
# Output 'ML\nand AI'
8
import re

# string with newline character
target_str = "ML\nand AI"

# Match any character
result = re.search(r".+", target_str)
print("Without using re.S flag:", result.group())
# Output 'ML'

# With re.S flag
result = re.search(r".+", target_str, re.S)
print("With re.S flag:", result.group())
# Output 'ML\nand AI'

# With re.DOTALL flag
result = re.search(r".+", target_str, re.DOTALL)
print("With re.DOTALL flag:", result.group())
# Output 'ML\nand AI'
9
Thực hiện kết hợp không phân biệt trường hợp phụ thuộc vào địa phương hiện tại. Chỉ sử dụng với các mẫu byte
Cờ Python Regex

Để chỉ định nhiều cờ, hãy sử dụng toán tử

import re

target_str = "Jessa is a Python developer, and her salary is 8000"

# re.X to add indentation  and comment in regex
result = re.search(r"""(^\w{2,}) # match 5-letter word at the start
                        .+(\d{4}$) # match 4-digit number at the end """, target_str, re.X)
# Fiver-letter word
print(result.group(1))
# Output 'Jessa'

# 4-digit number
print(result.group(2))
# Output 8000
0 để kết nối chúng. Ví dụ: trường hợp tìm kiếm không nhạy cảm trong chuỗi đa dòng

re.findall(pattern, string, flags=re.I|re.M|re.X)

Bây giờ, hãy để xem cách sử dụng từng cờ tùy chọn trong Python Regex.

Cờ Ignorecase

Trước hết, hãy để Lừa xem vai trò cờ

import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']
2, đại diện cho việc bỏ qua một trường hợp. chỉ định cờ này trong phương thức Regex là một đối số để thực hiện khớp không nhạy cảm với trường hợp. Bạn có thể chỉ định cờ này bằng hai cách

  1. import re
    
    target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"
    
    # Without using re.I
    result = re.findall(r"kelly", target_str)
    print(result)
    # Output ['kelly']
    
    # with re.I
    result = re.findall(r"kelly", target_str, re.I)
    print(result)
    # Output ['KELLy', 'kelly']
    
    # with re.IGNORECASE
    result = re.findall(r"kelly", target_str, re.IGNORECASE)
    print(result)
    # Output ['KELLy', 'kelly']
    2
  2. import re
    
    target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"
    
    # Without using re.I
    result = re.findall(r"kelly", target_str)
    print(result)
    # Output ['kelly']
    
    # with re.I
    result = re.findall(r"kelly", target_str, re.I)
    print(result)
    # Output ['KELLy', 'kelly']
    
    # with re.IGNORECASE
    result = re.findall(r"kelly", target_str, re.IGNORECASE)
    print(result)
    # Output ['KELLy', 'kelly']
    3

Thí dụ

import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']

Lưu ý từ từ Kelly Kelly, hai lần xảy ra bên trong chuỗi này., Thứ nhất, được viết hoa ở đầu các câu và thứ hai trong tất cả các chữ thường.kelly” the occurs two times inside this string., First, capitalized at the beginning of the sentences and second in all lowercase.

Trong phương thức

import re

target_str = "Jessa is a Python developer, and her salary is 8000"

# re.X to add indentation  and comment in regex
result = re.search(r"""(^\w{2,}) # match 5-letter word at the start
                        .+(\d{4}$) # match 4-digit number at the end """, target_str, re.X)
# Fiver-letter word
print(result.group(1))
# Output 'Jessa'

# 4-digit number
print(result.group(2))
# Output 8000
4 đầu tiên, chúng tôi chỉ có một lần xuất hiện bởi vì, theo mặc định, sự phù hợp là trường hợp nhạy cảm.

Và trong phương pháp

import re

target_str = "Jessa is a Python developer, and her salary is 8000"

# re.X to add indentation  and comment in regex
result = re.search(r"""(^\w{2,}) # match 5-letter word at the start
                        .+(\d{4}$) # match 4-digit number at the end """, target_str, re.X)
# Fiver-letter word
print(result.group(1))
# Output 'Jessa'

# 4-digit number
print(result.group(2))
# Output 8000
4 thứ hai, chúng tôi đã có 2 lần xuất hiện vì chúng tôi đã thay đổi hành vi nhạy cảm của trường hợp của Regex bằng cách sử dụng
import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']
2 để nó có thể tìm thấy tất cả các lần xuất hiện của một từ bất kể bất kỳ chữ cái nào của nó là chữ hoa hoặc chữ thường.

Cờ dotall

Bây giờ, hãy để Lừa xem vai trò cờ

import re

# string with newline character
target_str = "ML\nand AI"

# Match any character
result = re.search(r".+", target_str)
print("Without using re.S flag:", result.group())
# Output 'ML'

# With re.S flag
result = re.search(r".+", target_str, re.S)
print("With re.S flag:", result.group())
# Output 'ML\nand AI'

# With re.DOTALL flag
result = re.search(r".+", target_str, re.DOTALL)
print("With re.DOTALL flag:", result.group())
# Output 'ML\nand AI'
2. Bạn có thể chỉ định cờ này bằng hai cách

  1. import re
    
    # string with newline character
    target_str = "ML\nand AI"
    
    # Match any character
    result = re.search(r".+", target_str)
    print("Without using re.S flag:", result.group())
    # Output 'ML'
    
    # With re.S flag
    result = re.search(r".+", target_str, re.S)
    print("With re.S flag:", result.group())
    # Output 'ML\nand AI'
    
    # With re.DOTALL flag
    result = re.search(r".+", target_str, re.DOTALL)
    print("With re.DOTALL flag:", result.group())
    # Output 'ML\nand AI'
    2
  2. import re
    
    # string with newline character
    target_str = "ML\nand AI"
    
    # Match any character
    result = re.search(r".+", target_str)
    print("Without using re.S flag:", result.group())
    # Output 'ML'
    
    # With re.S flag
    result = re.search(r".+", target_str, re.S)
    print("With re.S flag:", result.group())
    # Output 'ML\nand AI'
    
    # With re.DOTALL flag
    result = re.search(r".+", target_str, re.DOTALL)
    print("With re.DOTALL flag:", result.group())
    # Output 'ML\nand AI'
    3

Như bạn đã biết, theo mặc định, Metacharacter DOT (____ 24) bên trong mẫu biểu thức chính quy đại diện cho bất kỳ ký tự nào, có thể là một chữ cái, chữ số, ký hiệu hoặc dấu chấm câu, ngoại trừ ký tự dòng mới, đó là

import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']
9.

Cờ

import re

# string with newline character
target_str = "ML\nand AI"

# Match any character
result = re.search(r".+", target_str)
print("Without using re.S flag:", result.group())
# Output 'ML'

# With re.S flag
result = re.search(r".+", target_str, re.S)
print("With re.S flag:", result.group())
# Output 'ML\nand AI'

# With re.DOTALL flag
result = re.search(r".+", target_str, re.DOTALL)
print("With re.DOTALL flag:", result.group())
# Output 'ML\nand AI'
2 làm cho ngoại lệ này biến mất bằng cách kích hoạt Metacharacter DOT (____ 24) để phù hợp với bất kỳ ký tự có thể nào, bao gồm cả ký tự dòng mới do đó tên của nó Dotall.DOTALL.

Điều này có thể chứng minh là khá hữu ích trong một số kịch bản, đặc biệt là khi chuỗi đích là đa dòng.

Bây giờ, hãy để sử dụng phương thức

import re

target_str = "Joy lucky number is 75\nTom lucky number is 25"

# find 3-letter word at the start of each newline
# Without re.M or re.MULTILINE flag
result = re.findall(r"^\w{3}", target_str)
print(result)  
# Output ['Joy']

# find 2-digit at the end of each newline
# Without re.M or re.MULTILINE flag
result = re.findall(r"\d{2}$", target_str)
print(result)
# Output ['25']

# With re.M or re.MULTILINE
# find 3-letter word at the start of each newline
result = re.findall(r"^\w{3}", target_str, re.MULTILINE)
print(result)
# Output ['Joy', 'Tom']

# With re.M
# find 2-digit number at the end of each newline
result = re.findall(r"\d{2}$", target_str, re.M)
print(result)
# Output ['75', '25']
4 có và không có cờ
import re

target_str = "Joy lucky number is 75\nTom lucky number is 25"

# find 3-letter word at the start of each newline
# Without re.M or re.MULTILINE flag
result = re.findall(r"^\w{3}", target_str)
print(result)  
# Output ['Joy']

# find 2-digit at the end of each newline
# Without re.M or re.MULTILINE flag
result = re.findall(r"\d{2}$", target_str)
print(result)
# Output ['25']

# With re.M or re.MULTILINE
# find 3-letter word at the start of each newline
result = re.findall(r"^\w{3}", target_str, re.MULTILINE)
print(result)
# Output ['Joy', 'Tom']

# With re.M
# find 2-digit number at the end of each newline
result = re.findall(r"\d{2}$", target_str, re.M)
print(result)
# Output ['75', '25']
5.

Thí dụ

import re

# string with newline character
target_str = "ML\nand AI"

# Match any character
result = re.search(r".+", target_str)
print("Without using re.S flag:", result.group())
# Output 'ML'

# With re.S flag
result = re.search(r".+", target_str, re.S)
print("With re.S flag:", result.group())
# Output 'ML\nand AI'

# With re.DOTALL flag
result = re.search(r".+", target_str, re.DOTALL)
print("With re.DOTALL flag:", result.group())
# Output 'ML\nand AI'

Lưu ý từ từ Kelly Kelly, hai lần xảy ra bên trong chuỗi này., Thứ nhất, được viết hoa ở đầu các câu và thứ hai trong tất cả các chữ thường.

Trong phương thức import re target_str = "Jessa is a Python developer, and her salary is 8000" # re.X to add indentation and comment in regex result = re.search(r"""(^\w{2,}) # match 5-letter word at the start .+(\d{4}$) # match 4-digit number at the end """, target_str, re.X) # Fiver-letter word print(result.group(1)) # Output 'Jessa' # 4-digit number print(result.group(2)) # Output 80004 đầu tiên, chúng tôi chỉ có một lần xuất hiện bởi vì, theo mặc định, sự phù hợp là trường hợp nhạy cảm.

Và trong phương pháp

import re

target_str = "Jessa is a Python developer, and her salary is 8000"

# re.X to add indentation  and comment in regex
result = re.search(r"""(^\w{2,}) # match 5-letter word at the start
                        .+(\d{4}$) # match 4-digit number at the end """, target_str, re.X)
# Fiver-letter word
print(result.group(1))
# Output 'Jessa'

# 4-digit number
print(result.group(2))
# Output 8000
4 thứ hai, chúng tôi đã có 2 lần xuất hiện vì chúng tôi đã thay đổi hành vi nhạy cảm của trường hợp của Regex bằng cách sử dụng
import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']
2 để nó có thể tìm thấy tất cả các lần xuất hiện của một từ bất kể bất kỳ chữ cái nào của nó là chữ hoa hoặc chữ thường.verbose. This flag allows more flexibility and better formatting when writing more complex regex patterns between the parentheses of the match(), search(), or other regex methods.

Cờ dotall

  1. import re
    
    # string with newline character
    target_str = "ML\nand AI"
    
    # Match any character
    result = re.search(r".+", target_str)
    print("Without using re.S flag:", result.group())
    # Output 'ML'
    
    # With re.S flag
    result = re.search(r".+", target_str, re.S)
    print("With re.S flag:", result.group())
    # Output 'ML\nand AI'
    
    # With re.DOTALL flag
    result = re.search(r".+", target_str, re.DOTALL)
    print("With re.DOTALL flag:", result.group())
    # Output 'ML\nand AI'
    6
  2. import re
    
    # string with newline character
    target_str = "ML\nand AI"
    
    # Match any character
    result = re.search(r".+", target_str)
    print("Without using re.S flag:", result.group())
    # Output 'ML'
    
    # With re.S flag
    result = re.search(r".+", target_str, re.S)
    print("With re.S flag:", result.group())
    # Output 'ML\nand AI'
    
    # With re.DOTALL flag
    result = re.search(r".+", target_str, re.DOTALL)
    print("With re.DOTALL flag:", result.group())
    # Output 'ML\nand AI'
    7

Bây giờ, hãy để Lừa xem vai trò cờ

import re

# string with newline character
target_str = "ML\nand AI"

# Match any character
result = re.search(r".+", target_str)
print("Without using re.S flag:", result.group())
# Output 'ML'

# With re.S flag
result = re.search(r".+", target_str, re.S)
print("With re.S flag:", result.group())
# Output 'ML\nand AI'

# With re.DOTALL flag
result = re.search(r".+", target_str, re.DOTALL)
print("With re.DOTALL flag:", result.group())
# Output 'ML\nand AI'
2. Bạn có thể chỉ định cờ này bằng hai cách

  • Như bạn đã biết, theo mặc định, Metacharacter DOT (____ 24) bên trong mẫu biểu thức chính quy đại diện cho bất kỳ ký tự nào, có thể là một chữ cái, chữ số, ký hiệu hoặc dấu chấm câu, ngoại trừ ký tự dòng mới, đó là
    import re
    
    target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"
    
    # Without using re.I
    result = re.findall(r"kelly", target_str)
    print(result)
    # Output ['kelly']
    
    # with re.I
    result = re.findall(r"kelly", target_str, re.I)
    print(result)
    # Output ['KELLy', 'kelly']
    
    # with re.IGNORECASE
    result = re.findall(r"kelly", target_str, re.IGNORECASE)
    print(result)
    # Output ['KELLy', 'kelly']
    9.
  • Cờ
    import re
    
    # string with newline character
    target_str = "ML\nand AI"
    
    # Match any character
    result = re.search(r".+", target_str)
    print("Without using re.S flag:", result.group())
    # Output 'ML'
    
    # With re.S flag
    result = re.search(r".+", target_str, re.S)
    print("With re.S flag:", result.group())
    # Output 'ML\nand AI'
    
    # With re.DOTALL flag
    result = re.search(r".+", target_str, re.DOTALL)
    print("With re.DOTALL flag:", result.group())
    # Output 'ML\nand AI'
    2 làm cho ngoại lệ này biến mất bằng cách kích hoạt Metacharacter DOT (____ 24) để phù hợp với bất kỳ ký tự có thể nào, bao gồm cả ký tự dòng mới do đó tên của nó Dotall.add comments right inside the pattern for later reference using the hash sign (#).

Điều này có thể chứng minh là khá hữu ích trong một số kịch bản, đặc biệt là khi chuỗi đích là đa dòng.

Bây giờ, hãy để sử dụng phương thức

import re

target_str = "Joy lucky number is 75\nTom lucky number is 25"

# find 3-letter word at the start of each newline
# Without re.M or re.MULTILINE flag
result = re.findall(r"^\w{3}", target_str)
print(result)  
# Output ['Joy']

# find 2-digit at the end of each newline
# Without re.M or re.MULTILINE flag
result = re.findall(r"\d{2}$", target_str)
print(result)
# Output ['25']

# With re.M or re.MULTILINE
# find 3-letter word at the start of each newline
result = re.findall(r"^\w{3}", target_str, re.MULTILINE)
print(result)
# Output ['Joy', 'Tom']

# With re.M
# find 2-digit number at the end of each newline
result = re.findall(r"\d{2}$", target_str, re.M)
print(result)
# Output ['75', '25']
4 có và không có cờ
import re

target_str = "Joy lucky number is 75\nTom lucky number is 25"

# find 3-letter word at the start of each newline
# Without re.M or re.MULTILINE flag
result = re.findall(r"^\w{3}", target_str)
print(result)  
# Output ['Joy']

# find 2-digit at the end of each newline
# Without re.M or re.MULTILINE flag
result = re.findall(r"\d{2}$", target_str)
print(result)
# Output ['25']

# With re.M or re.MULTILINE
# find 3-letter word at the start of each newline
result = re.findall(r"^\w{3}", target_str, re.MULTILINE)
print(result)
# Output ['Joy', 'Tom']

# With re.M
# find 2-digit number at the end of each newline
result = re.findall(r"\d{2}$", target_str, re.M)
print(result)
# Output ['75', '25']
5.

import re

target_str = "Jessa is a Python developer, and her salary is 8000"

# re.X to add indentation  and comment in regex
result = re.search(r"""(^\w{2,}) # match 5-letter word at the start
                        .+(\d{4}$) # match 4-digit number at the end """, target_str, re.X)
# Fiver-letter word
print(result.group(1))
# Output 'Jessa'

# 4-digit number
print(result.group(2))
# Output 8000

Trong cuộc gọi đầu tiên của phương thức import re target_str = "Joy lucky number is 75\nTom lucky number is 25" # find 3-letter word at the start of each newline # Without re.M or re.MULTILINE flag result = re.findall(r"^\w{3}", target_str) print(result) # Output ['Joy'] # find 2-digit at the end of each newline # Without re.M or re.MULTILINE flag result = re.findall(r"\d{2}$", target_str) print(result) # Output ['25'] # With re.M or re.MULTILINE # find 3-letter word at the start of each newline result = re.findall(r"^\w{3}", target_str, re.MULTILINE) print(result) # Output ['Joy', 'Tom'] # With re.M # find 2-digit number at the end of each newline result = re.findall(r"\d{2}$", target_str, re.M) print(result) # Output ['75', '25']4, Dot didn nhận ra import re target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI" # Without using re.I result = re.findall(r"kelly", target_str) print(result) # Output ['kelly'] # with re.I result = re.findall(r"kelly", target_str, re.I) print(result) # Output ['KELLy', 'kelly'] # with re.IGNORECASE result = re.findall(r"kelly", target_str, re.IGNORECASE) print(result) # Output ['KELLy', 'kelly']9 và ngừng khớp. Sau khi thêm cờ tùy chọn import re # string with newline character target_str = "ML\nand AI" # Match any character result = re.search(r".+", target_str) print("Without using re.S flag:", result.group()) # Output 'ML' # With re.S flag result = re.search(r".+", target_str, re.S) print("With re.S flag:", result.group()) # Output 'ML\nand AI' # With re.DOTALL flag result = re.search(r".+", target_str, re.DOTALL) print("With re.DOTALL flag:", result.group()) # Output 'ML\nand AI'2 trong cuộc gọi tiếp theo, ký tự DOT khớp với toàn bộ chuỗi.

Cờ dotall

  1. import re
    
    target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"
    
    # Without using re.I
    result = re.findall(r"kelly", target_str)
    print(result)
    # Output ['kelly']
    
    # with re.I
    result = re.findall(r"kelly", target_str, re.I)
    print(result)
    # Output ['KELLy', 'kelly']
    
    # with re.IGNORECASE
    result = re.findall(r"kelly", target_str, re.IGNORECASE)
    print(result)
    # Output ['KELLy', 'kelly']
    4
  2. import re
    
    target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"
    
    # Without using re.I
    result = re.findall(r"kelly", target_str)
    print(result)
    # Output ['kelly']
    
    # with re.I
    result = re.findall(r"kelly", target_str, re.I)
    print(result)
    # Output ['KELLy', 'kelly']
    
    # with re.IGNORECASE
    result = re.findall(r"kelly", target_str, re.IGNORECASE)
    print(result)
    # Output ['KELLy', 'kelly']
    5

Bây giờ, hãy để Lừa xem vai trò cờ

import re

# string with newline character
target_str = "ML\nand AI"

# Match any character
result = re.search(r".+", target_str)
print("Without using re.S flag:", result.group())
# Output 'ML'

# With re.S flag
result = re.search(r".+", target_str, re.S)
print("With re.S flag:", result.group())
# Output 'ML\nand AI'

# With re.DOTALL flag
result = re.search(r".+", target_str, re.DOTALL)
print("With re.DOTALL flag:", result.group())
# Output 'ML\nand AI'
2. Bạn có thể chỉ định cờ này bằng hai cách

Như bạn đã biết, theo mặc định, Metacharacter DOT (____ 24) bên trong mẫu biểu thức chính quy đại diện cho bất kỳ ký tự nào, có thể là một chữ cái, chữ số, ký hiệu hoặc dấu chấm câu, ngoại trừ ký tự dòng mới, đó là

import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']
9.

  1. CARET (________ 16) chỉ khớp với một mẫu ở đầu chuỗi
  2. Đồng đô la (
    import re
    
    target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"
    
    # Without using re.I
    result = re.findall(r"kelly", target_str)
    print(result)
    # Output ['kelly']
    
    # with re.I
    result = re.findall(r"kelly", target_str, re.I)
    print(result)
    # Output ['KELLy', 'kelly']
    
    # with re.IGNORECASE
    result = re.findall(r"kelly", target_str, re.IGNORECASE)
    print(result)
    # Output ['KELLy', 'kelly']
    7) phù hợp với mẫu biểu thức chính quy ở cuối chuỗi

Khi cờ này được chỉ định, ký tự mẫu

import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']
6 khớp với đầu chuỗi và mỗi lần bắt đầu dòng mới (
import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']
9). Và ký tự metacharacter
import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']
7 phù hợp ở cuối chuỗi và kết thúc của mỗi dòng mới (
import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']
9).

Bây giờ, hãy để xem các ví dụ.

import re

target_str = "Joy lucky number is 75\nTom lucky number is 25"

# find 3-letter word at the start of each newline
# Without re.M or re.MULTILINE flag
result = re.findall(r"^\w{3}", target_str)
print(result)  
# Output ['Joy']

# find 2-digit at the end of each newline
# Without re.M or re.MULTILINE flag
result = re.findall(r"\d{2}$", target_str)
print(result)
# Output ['25']

# With re.M or re.MULTILINE
# find 3-letter word at the start of each newline
result = re.findall(r"^\w{3}", target_str, re.MULTILINE)
print(result)
# Output ['Joy', 'Tom']

# With re.M
# find 2-digit number at the end of each newline
result = re.findall(r"\d{2}$", target_str, re.M)
print(result)
# Output ['75', '25']

Cờ Ascii

Bạn có thể chỉ định cờ này bằng hai cách

  1. import re
    
    target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"
    
    # Without using re.I
    result = re.findall(r"kelly", target_str)
    print(result)
    # Output ['kelly']
    
    # with re.I
    result = re.findall(r"kelly", target_str, re.I)
    print(result)
    # Output ['KELLy', 'kelly']
    
    # with re.IGNORECASE
    result = re.findall(r"kelly", target_str, re.IGNORECASE)
    print(result)
    # Output ['KELLy', 'kelly']
    0
  2. import re
    
    target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"
    
    # Without using re.I
    result = re.findall(r"kelly", target_str)
    print(result)
    # Output ['kelly']
    
    # with re.I
    result = re.findall(r"kelly", target_str, re.I)
    print(result)
    # Output ['KELLy', 'kelly']
    
    # with re.IGNORECASE
    result = re.findall(r"kelly", target_str, re.IGNORECASE)
    print(result)
    # Output ['KELLy', 'kelly']
    1

Làm cho regex & nbsp; ________ 69, & nbsp; ________ 70, & nbsp; ________ 71, & nbsp; Điều này chỉ có ý nghĩa đối với các mẫu Unicode và bị bỏ qua cho các mẫu byte.

import re

# string with ASCII and Unicode characters
target_str = "虎太郎 and Jessa are friends"

# Without re.A or re.ASCII
# To match all 3-letter word
result = re.findall(r"\b\w{3}\b", target_str)
print(result)
# Output ['虎太郎', 'and', 'are']

# With re.A or re.ASCII
# regex to match only 3-letter ASCII word
result = re.findall(r"\b\w{3}\b", target_str, re.A)
print(result)
# Output ['and', 'are']

Cờ trong Regex Python là gì?

Python Regex cho phép các cờ tùy chọn chỉ định khi sử dụng các mẫu biểu thức thông thường với match (), search () và split (), trong số các mẫu khác. Tất cả các phương thức mô -đun RE đều chấp nhận một đối số cờ tùy chọn cho phép các tính năng và biến thể cú pháp độc đáo khác nhau.allows optional flags to specify when using regular expression patterns with match() , search() , and split() , among others. All RE module methods accept an optional flags argument that enables various unique features and syntax variations.

Cờ Regex là gì?

Một biểu thức chính quy bao gồm một mẫu và cờ tùy chọn: g, i, m, u, s, y.Không có cờ và các biểu tượng đặc biệt (mà chúng ta sẽ nghiên cứu sau), việc tìm kiếm bởi RegEXP giống như tìm kiếm chuỗi con.Phương pháp str.Match (RegEXP) tìm kiếm các trận đấu: tất cả chúng nếu có cờ G, nếu không, chỉ có lần đầu tiên.g , i , m , u , s , y . Without flags and special symbols (that we'll study later), the search by a regexp is the same as a substring search. The method str. match(regexp) looks for matches: all of them if there's g flag, otherwise, only the first one.

Những sửa đổi nào được sử dụng trong biểu thức chính quy?

Có ba sửa đổi cơ bản, hoặc cờ, cho regexp, chúng là i, g, m.i, g, m.

Hai loại ký tự được sử dụng trong biểu thức thông thường trong Python là gì?

Biểu thức chính quy sử dụng hai loại ký tự trong chuỗi mẫu phù hợp: Các ký tự meta là các ký tự có ý nghĩa đặc biệt, tương tự như * trong thẻ hoang dã. Các ký tự là các ký tự chữ và số.Meta characters are characters having a special meaning, similar to * in wild card. Literals are alphanumeric characters.