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[]
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.
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.
0 | 1 | Thực hiện kết hợp ASCII-chỉ thay vì khớp unicode đầy đủ |
2 | 3 | Thực hiện kết hợp không nhạy cảm trường hợp |
4 | 5 | Cờ này được sử dụng với Metacharacter 6 [CARET] và 7 [Dollar]. Khi cờ này được chỉ định, Metacharacter 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 7 của chuỗi và kết thúc của mỗi dòng mới [ 9]When this flag is specified, the metacharacter 6 matches the pattern at beginning of the string and each newline’s beginning [ 9].And the metacharacter 7 matches pattern at the end of the string and the end of each new line [ 9]
|
2 | 3 | Làm cho ký tự đặc biệt DOT [ 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 |
6 | 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. |
8 | 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 |
Để 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òngre.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
2import 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']
3import 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']
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
2import 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'
3import 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'
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 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.
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
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
6import 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'
7import 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'
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à
9.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']
- Cờ
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 [#].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'
Đ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.
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']
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']
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'
Cờ dotall
4import 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']
5import 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']
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áchNhư 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à
9.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']
- CARET [________ 16] chỉ khớp với một mẫu ở đầu chuỗi
- Đồng đô la [
7] phù hợp với mẫu biểu thức chính quy ở cuối chuỗiimport 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']
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
0import 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']
1import 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à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']