Hướng dẫn regex match word with special characters python - regex khớp từ với ký tự đặc biệt python
Đối với chuỗi ví dụ bạn đưa ra, biểu thức chính quy sau hoạt động OK: Show
... hoặc bạn có thể xóa những từ đó bằng
Cập nhật: Tất nhiên, như các câu trả lời khác hoàn toàn đề xuất, vì tiền tố 0) mà bạn không cần phải thay thế đó - bạn có thể đơn giản hóa biểu thức thông thường thành 1. Tuy nhiên, có lẽ ví dụ trên với sự xen kẽ vẫn còn hữu ích. Biểu cảm thường xuyên là một con vật kỳ lạ. Nhiều sinh viên thấy họ khó hiểu - phải không? Regex Nhân vật đặc biệt - Ví dụ trong Python Re Tôi nhận ra rằng một lý do chính cho điều này chỉ đơn giản là họ không hiểu các nhân vật Regex đặc biệt. Nói cách khác: Hiểu các nhân vật đặc biệt và mọi thứ khác trong không gian Regex sẽ dễ dàng hơn nhiều với bạn. Bài viết liên quan: Siêu cường Python Regex - Hướng dẫn cuối cùng Python Regex Superpower – The Ultimate Guide Bạn có muốn thành thạo siêu cường Regex không? Kiểm tra cuốn sách mới của tôi Cách thông minh nhất để học các biểu thức thường xuyên trong Python với cách tiếp cận 3 bước sáng tạo để học tập tích cực: (1) Nghiên cứu một chương sách, (2) Giải câu đố mã và (3) xem video chương giáo dục . Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video. Biểu thức chính quy được xây dựng từ các ký tự. Có hai loại ký tự: ký tự theo nghĩa đen và các ký tự đặc biệt.literal characters and special characters. Ký tự nghĩa đenHãy để bắt đầu với điều đầu tiên tuyệt đối mà bạn cần biết với các biểu thức thông thường: một biểu thức thông thường (ngắn: regex) tìm kiếm cho một mẫu nhất định trong một chuỗi nhất định. Những gì một mô hình? Ở dạng cơ bản nhất của nó, một mẫu có thể là một nhân vật theo nghĩa đen. Vì vậy, các ký tự theo nghĩa đen 2, 3 và 4 đều là các mẫu regex hợp lệ.Ví dụ: bạn có thể tìm kiếm mẫu regex 2 trong chuỗi 6 nhưng nó đã giành được một trận đấu. Bạn cũng có thể tìm kiếm mẫu 2 trong chuỗi 8 và có một trận đấu: ký tự cuối cùng thứ hai trong chuỗi.Dựa trên cái nhìn sâu sắc đơn giản rằng một ký tự theo nghĩa đen là mẫu Regex hợp lệ, bạn sẽ thấy rằng sự kết hợp của các ký tự theo nghĩa đen cũng là một mẫu Regex hợp lệ. Ví dụ: mẫu regex 9 khớp với hai ký tự cuối cùng trong chuỗi 8.Tóm tắt: Biểu thức chính quy được xây dựng từ các ký tự. Một lớp quan trọng của các ký tự là các ký tự theo nghĩa đen. Về nguyên tắc, bạn có thể sử dụng tất cả các ký tự theo nghĩa đen của Unicode trong mẫu Regex của bạn.: Regular expressions are built from characters. An important class of characters are the literal characters. In principle, you can use all Unicode literal characters in your regex pattern. Tuy nhiên, sức mạnh của các biểu hiện thông thường đến từ khả năng trừu tượng của chúng. Thay vì viết bộ ký tự import re text = ''' Ha! let me see her: out, alas! he's cold: Her blood is settled, and her joints are stiff; Life and these lips have long been separated: Death lies on her like an untimely frost Upon the sweetest flower of all the field. ''' print(re.findall('.a!', text)) ''' Finds all occurrences of an arbitrary character that is followed by the character sequence 'a!'. ['Ha!'] ''' print(re.findall('is.*and', text)) ''' Finds all occurrences of the word 'is', followed by an arbitrary number of characters and the word 'and'. ['is settled, and'] ''' print(re.findall('her:?', text)) ''' Finds all occurrences of the word 'her', followed by zero or one occurrences of the colon ':'. ['her:', 'her', 'her'] ''' print(re.findall('her:+', text)) ''' Finds all occurrences of the word 'her', followed by one or more occurrences of the colon ':'. ['her:'] ''' print(re.findall('^Ha.*', text)) ''' Finds all occurrences where the string starts with the character sequence 'Ha', followed by an arbitrary number of characters except for the new-line character. Can you figure out why Python doesn't find any? [] ''' print(re.findall('\n$', text)) ''' Finds all occurrences where the new-line character '\n' occurs at the end of the string. ['\n'] ''' print(re.findall('(Life|Death)', text)) ''' Finds all occurrences of either the word 'Life' or the word 'Death'. ['Life', 'Death'] '''1, bạn đã viết import re text = ''' Ha! let me see her: out, alas! he's cold: Her blood is settled, and her joints are stiff; Life and these lips have long been separated: Death lies on her like an untimely frost Upon the sweetest flower of all the field. ''' print(re.findall('.a!', text)) ''' Finds all occurrences of an arbitrary character that is followed by the character sequence 'a!'. ['Ha!'] ''' print(re.findall('is.*and', text)) ''' Finds all occurrences of the word 'is', followed by an arbitrary number of characters and the word 'and'. ['is settled, and'] ''' print(re.findall('her:?', text)) ''' Finds all occurrences of the word 'her', followed by zero or one occurrences of the colon ':'. ['her:', 'her', 'her'] ''' print(re.findall('her:+', text)) ''' Finds all occurrences of the word 'her', followed by one or more occurrences of the colon ':'. ['her:'] ''' print(re.findall('^Ha.*', text)) ''' Finds all occurrences where the string starts with the character sequence 'Ha', followed by an arbitrary number of characters except for the new-line character. Can you figure out why Python doesn't find any? [] ''' print(re.findall('\n$', text)) ''' Finds all occurrences where the new-line character '\n' occurs at the end of the string. ['\n'] ''' print(re.findall('(Life|Death)', text)) ''' Finds all occurrences of either the word 'Life' or the word 'Death'. ['Life', 'Death'] '''2 hoặc thậm chí import re text = ''' Ha! let me see her: out, alas! he's cold: Her blood is settled, and her joints are stiff; Life and these lips have long been separated: Death lies on her like an untimely frost Upon the sweetest flower of all the field. ''' print(re.findall('.a!', text)) ''' Finds all occurrences of an arbitrary character that is followed by the character sequence 'a!'. ['Ha!'] ''' print(re.findall('is.*and', text)) ''' Finds all occurrences of the word 'is', followed by an arbitrary number of characters and the word 'and'. ['is settled, and'] ''' print(re.findall('her:?', text)) ''' Finds all occurrences of the word 'her', followed by zero or one occurrences of the colon ':'. ['her:', 'her', 'her'] ''' print(re.findall('her:+', text)) ''' Finds all occurrences of the word 'her', followed by one or more occurrences of the colon ':'. ['her:'] ''' print(re.findall('^Ha.*', text)) ''' Finds all occurrences where the string starts with the character sequence 'Ha', followed by an arbitrary number of characters except for the new-line character. Can you figure out why Python doesn't find any? [] ''' print(re.findall('\n$', text)) ''' Finds all occurrences where the new-line character '\n' occurs at the end of the string. ['\n'] ''' print(re.findall('(Life|Death)', text)) ''' Finds all occurrences of either the word 'Life' or the word 'Death'. ['Life', 'Death'] '''3. Sau này là một nhân vật Regex đặc biệt, và người chuyên nghiệp biết họ bằng trái tim. Trong thực tế, các chuyên gia Regex hiếm khi phù hợp với các nhân vật theo nghĩa đen. Trong hầu hết các trường hợp, họ sử dụng các cấu trúc nâng cao hơn hoặc các ký tự đặc biệt vì nhiều lý do khác nhau như ngắn gọn, biểu cảm hoặc tính tổng quát. Vậy các ký tự đặc biệt bạn có thể sử dụng trong các mẫu Regex của mình là gì? Hãy để một cái nhìn vào bảng sau đây chứa tất cả các ký tự đặc biệt trong gói Python, ____24 để xử lý biểu thức thông thường.
Nhưng đây không phải là tất cả các ký tự bạn có thể sử dụng trong một biểu thức thông thường. Ngoài ra còn có các ký tự meta cho động cơ Regex cho phép bạn làm những thứ mạnh mẽ hơn nhiều. Một ví dụ điển hình là toán tử Asterisk phù hợp với các lần xuất hiện của Zero hoặc nhiều hơn của Regex trước đó. Ví dụ: mẫu Ký tự meta regexHãy xem video ngắn về các ký tự Regex Meta quan trọng nhất: Cú pháp Python Regex [mồi 15 phút] Tiếp theo, bạn sẽ nhận được một cái nhìn tổng quan nhanh chóng và bẩn thỉu về các hoạt động Regex quan trọng nhất và cách sử dụng chúng trong Python. Dưới đây là các nhà khai thác Regex quan trọng nhất:
Lưu ý rằng tôi đã cho các toán tử ở trên một số tên có ý nghĩa hơn (in đậm) để bạn có thể ngay lập tức nắm bắt mục đích của mỗi regex. Ví dụ, toán tử 14 thường được ký hiệu là toán tử ‘caret. Những cái tên đó không phải là mô tả nên tôi đã đưa ra những từ giống như mẫu giáo hơn như toán tử khởi động trên mạng.Hãy để đi sâu vào một số ví dụ! Ví dụimport re text = ''' Ha! let me see her: out, alas! he's cold: Her blood is settled, and her joints are stiff; Life and these lips have long been separated: Death lies on her like an untimely frost Upon the sweetest flower of all the field. ''' print(re.findall('.a!', text)) ''' Finds all occurrences of an arbitrary character that is followed by the character sequence 'a!'. ['Ha!'] ''' print(re.findall('is.*and', text)) ''' Finds all occurrences of the word 'is', followed by an arbitrary number of characters and the word 'and'. ['is settled, and'] ''' print(re.findall('her:?', text)) ''' Finds all occurrences of the word 'her', followed by zero or one occurrences of the colon ':'. ['her:', 'her', 'her'] ''' print(re.findall('her:+', text)) ''' Finds all occurrences of the word 'her', followed by one or more occurrences of the colon ':'. ['her:'] ''' print(re.findall('^Ha.*', text)) ''' Finds all occurrences where the string starts with the character sequence 'Ha', followed by an arbitrary number of characters except for the new-line character. Can you figure out why Python doesn't find any? [] ''' print(re.findall('\n$', text)) ''' Finds all occurrences where the new-line character '\n' occurs at the end of the string. ['\n'] ''' print(re.findall('(Life|Death)', text)) ''' Finds all occurrences of either the word 'Life' or the word 'Death'. ['Life', 'Death'] ''' Trong các ví dụ này, bạn đã thấy biểu tượng đặc biệt import re text = ''' Ha! let me see her: out, alas! he's cold: Her blood is settled, and her joints are stiff; Life and these lips have long been separated: Death lies on her like an untimely frost Upon the sweetest flower of all the field. ''' print(re.findall('.a!', text)) ''' Finds all occurrences of an arbitrary character that is followed by the character sequence 'a!'. ['Ha!'] ''' print(re.findall('is.*and', text)) ''' Finds all occurrences of the word 'is', followed by an arbitrary number of characters and the word 'and'. ['is settled, and'] ''' print(re.findall('her:?', text)) ''' Finds all occurrences of the word 'her', followed by zero or one occurrences of the colon ':'. ['her:', 'her', 'her'] ''' print(re.findall('her:+', text)) ''' Finds all occurrences of the word 'her', followed by one or more occurrences of the colon ':'. ['her:'] ''' print(re.findall('^Ha.*', text)) ''' Finds all occurrences where the string starts with the character sequence 'Ha', followed by an arbitrary number of characters except for the new-line character. Can you figure out why Python doesn't find any? [] ''' print(re.findall('\n$', text)) ''' Finds all occurrences where the new-line character '\n' occurs at the end of the string. ['\n'] ''' print(re.findall('(Life|Death)', text)) ''' Finds all occurrences of either the word 'Life' or the word 'Death'. ['Life', 'Death'] '''5 biểu thị ký tự dòng mới trong Python (và hầu hết các ngôn ngữ khác). Có nhiều ký tự đặc biệt, được thiết kế đặc biệt cho các biểu thức thông thường. Những nhân vật Python Regex đặc biệt nào phải được trốn thoát?Câu trả lời ngắn gọn: Ở đây, một danh sách đầy đủ của tất cả các nhân vật đặc biệt cần được thoát ra:: Here’s an exhaustive list of all special characters that need to be escaped:
Câu hỏi: Có một danh sách toàn diện về những nhân vật đặc biệt phải được thoát ra để loại bỏ ý nghĩa đặc biệt trong regex?: Is there a comprehensive list of which special characters must be escaped in order to remove the special meaning within the regex? Ví dụ: Giả sử bạn tìm kiếm các biểu tượng đó trong một chuỗi đã cho và bạn tự hỏi bạn phải trốn thoát nào trong số chúng:: Say you search for those symbols in a given string and you wonder which of them you must escape: |^&+-%*/=!> Trả lời: Phân biệt giữa việc sử dụng các ký hiệu đặc biệt trong hoặc bên ngoài một lớp ký tự.: Differentiate between using the special symbols within or outside a character class.
import re text = '|^&+-%*/=!>' # WITHIN CHARACTER CLASS --> ESCAPE '-' print(re.findall('[|^&+\-%*/=!>]', text)) # ['|', '^', '&', '+', '-', '%', '*', '/', '=', '!', '>'] # WITHOUT CHARACTER CLASS --> ESCAPE ALL SPECIAL CHARS '.*?+^$|' pattern = '|^&+$-%*/=!>' print(re.findall('\|', text)) print(re.findall('\^', text)) print(re.findall('\$', text)) print(re.findall('\+', text)) print(re.findall('-', text)) print(re.findall('%', text)) print(re.findall('\*', text)) print(re.findall('/', text)) print(re.findall('=', text)) print(re.findall('!', text)) ''' ['|'] ['^'] ['$'] ['+'] ['-'] ['%'] ['*'] ['/'] ['='] ['!'] ''' Bằng cách thoát khỏi các biểu tượng Regex đặc biệt, chúng mất đi ý nghĩa đặc biệt của chúng và bạn có thể tìm thấy các biểu tượng trong văn bản gốc. Đi đâu từ đâyBạn đã học được tất cả các nhân vật đặc biệt của các biểu thức thông thường, cũng như các ký tự meta. Điều này sẽ cung cấp cho bạn một cơ sở mạnh mẽ để cải thiện kỹ năng regex của bạn. Nếu bạn muốn tăng tốc các kỹ năng của mình, bạn cần một nền tảng tốt. Kiểm tra cuốn sách Python hoàn toàn mới của tôi, Py Python One-Liners (Amazon Link), giúp tăng các kỹ năng của bạn từ số 0 đến Hero, trong một dòng mã Python duy nhất! Regex hài hướcĐợi đã, quên thoát một không gian. Wheeeeee [Taptaptap] Eeeeee. (nguồn)Trong khi làm việc như một nhà nghiên cứu trong các hệ thống phân tán, Tiến sĩ Christian Mayer đã tìm thấy tình yêu của mình đối với việc dạy các sinh viên khoa học máy tính. Để giúp học sinh đạt được thành công cao hơn của Python, ông đã thành lập trang web giáo dục chương trình Finxter.com. Ông là tác giả của cuốn sách lập trình phổ biến Python Oneer (Nostarch 2020), đồng tác giả của loạt sách Break Break Python, những cuốn sách tự xuất bản, người đam mê khoa học máy tính, freelancer và chủ sở hữu của một trong 10 blog Python lớn nhất trên toàn thế giới. Niềm đam mê của ông là viết, đọc và mã hóa. Nhưng niềm đam mê lớn nhất của anh là phục vụ các lập trình viên đầy tham vọng thông qua Finxter và giúp họ tăng cường các kỹ năng của họ. Bạn có thể tham gia học viện email miễn phí của anh ấy ở đây. '\ S +' trong Python là gì?Vì \ s+ có nghĩa là một chuỗi các ký tự không phải là màu trắng và \ s+ có nghĩa là một chuỗi các ký tự khoảng trắng, điều này phù hợp chính xác là một phần của đầu ra.a string of non-whitespace characters” and \s+ means “a string of whitespace characters”, this correctly matches that part of the output.
Điều này có nghĩa là gì trong regex ([]) \ 1?Đây là thẻ HTML mở. . \ 1 khớp với cùng một văn bản được khớp bởi nhóm chụp đầu tiên. / Trước đó là một nhân vật theo nghĩa đen.references the first capturing group. \1 matches the exact same text that was matched by the first capturing group. The / before it is a literal character.
\ B trong python regex là gì?Bên trong một phạm vi ký tự, \ B đại diện cho ký tự backspace, để tương thích với các chữ viết của Python.Khớp với chuỗi trống, nhưng chỉ khi nó không ở đầu hoặc cuối của một từ.backspace character, for compatibility with Python's string literals. Matches the empty string, but only when it is not at the beginning or end of a word.
Là gì '?'Trong biểu hiện chính quy?có nghĩa là "0 hoặc một chữ số, nhưng không phải hai hoặc nhiều".[0-9]* có nghĩa là "0 hoặc nhiều chữ số (không có giới hạn, có thể là 42 trong số đó)".Lưu ý rằng một số ngôn ngữ yêu cầu phao được viết với 0 hàng đầu trước.Nếu số là từ 0 đến 1 (0,5 không.zero or one digits, but not two or more". [0-9]* means "zero or more digits (no limit, could be 42 of them)". Note that some languages require that floats are written with a leading 0 before the . if the number is between 0 and 1 ( 0.5 not . |