Python RE [Gex]?
Chương này sẽ chỉ ra cách phù hợp với Metacharacters theo nghĩa đen, cho các mẫu được xây dựng bằng tay cũng như lập trình. Bạn cũng sẽ tìm hiểu về các chuỗi thoát được hỗ trợ bởi mô -đun
>>> eqn = 'f*[a^b] - 3*[a^b]'
# straightforward search and replace, no need RE shenanigans
>>> eqn.replace['[a^b]', 'c']
'f*c - 3*c'
3.Trốn thoát với \
Bạn đã thấy một vài metacharacters và các trình tự thoát giúp sáng tác lại. Để phù hợp với các metacharacters theo nghĩa đen, tức là để loại bỏ ý nghĩa đặc biệt của chúng, tiền tố các ký tự đó có ký tự
>>> eqn = 'f*[a^b] - 3*[a^b]'
# straightforward search and replace, no need RE shenanigans
>>> eqn.replace['[a^b]', 'c']
'f*c - 3*c'
4 [dấu gạch chéo ngược]. Để chỉ ra một ký tự >>> eqn = 'f*[a^b] - 3*[a^b]'
# straightforward search and replace, no need RE shenanigans
>>> eqn.replace['[a^b]', 'c']
'f*c - 3*c'
4 theo nghĩa đen, hãy sử dụng >>> eqn = 'f*[a^b] - 3*[a^b]'
# straightforward search and replace, no need RE shenanigans
>>> eqn.replace['[a^b]', 'c']
'f*c - 3*c'
6. Giả sử đây là tất cả các phần của chuỗi thô, không phải chuỗi bình thường.# even though ^ is not being used as anchor, it won't be matched literally
>>> bool[re.search[r'b^2', 'a^2 + b^2 - C*3']]
False
# escaping will work
>>> bool[re.search[r'b\^2', 'a^2 + b^2 - C*3']]
True
# match [ or ] literally
>>> re.sub[r'\[|\]', '', '[a*b] + c']
'a*b + c'
# note that here input string is also a raw string
>>> re.sub[r'\\', '/', r'\learn\by\example']
'/learn/by/example'
Như đã nhấn mạnh trước đó, các biểu thức chính quy chỉ là một công cụ khác để xử lý văn bản. Một số ví dụ và bài tập được trình bày trong cuốn sách này cũng có thể được giải quyết bằng các phương thức chuỗi bình thường. Đối với các trường hợp sử dụng trong thế giới thực, hãy tự hỏi nếu cần biểu hiện thông thường?
>>> eqn = 'f*[a^b] - 3*[a^b]'
# straightforward search and replace, no need RE shenanigans
>>> eqn.replace['[a^b]', 'c']
'f*c - 3*c'
re.escape
Được rồi, điều gì sẽ xảy ra nếu bạn có một biến chuỗi phải được sử dụng để xây dựng một RE - làm thế nào để thoát khỏi tất cả các metacharacters? Thư giãn, chức năng
>>> eqn = 'f*[a^b] - 3*[a^b]'
# straightforward search and replace, no need RE shenanigans
>>> eqn.replace['[a^b]', 'c']
'f*c - 3*c'
7 đã giúp bạn được bảo hiểm. Không cần phải chăm sóc tất cả các metacharacters hoặc lo lắng về những thay đổi trong các phiên bản trong tương lai.>>> expr = '[a^b]'
# print used here to show results similar to raw string
>>> print[re.escape[expr]]
\[a\^b\]
# replace only at end of string
>>> eqn = 'f*[a^b] - 3*[a^b]'
>>> re.sub[re.escape[expr] + r'\Z', 'c', eqn]
'f*[a^b] - 3*c'
Hãy nhớ lại rằng trong phần xen kẽ,
>>> eqn = 'f*[a^b] - 3*[a^b]'
# straightforward search and replace, no need RE shenanigans
>>> eqn.replace['[a^b]', 'c']
'f*c - 3*c'
8 đã được sử dụng để xây dựng động mẫu RE từ một chuỗi có thể lặp lại. Tuy nhiên, điều đó không xử lý các metacharacters. Đây là cách bạn có thể sử dụng >>> eqn = 'f*[a^b] - 3*[a^b]'
# straightforward search and replace, no need RE shenanigans
>>> eqn.replace['[a^b]', 'c']
'f*c - 3*c'
7 để mẫu kết quả sẽ khớp với các chuỗi từ đầu vào có thể theo nghĩa đen.# iterable of strings, assume alternation precedence sorting isn't needed
>>> terms = ['a_42', '[a^b]', '2|3']
# using 're.escape' and 'join' to construct the pattern
>>> pat1 = re.compile['|'.join[re.escape[s] for s in terms]]
# using only 'join' to construct the pattern
>>> pat2 = re.compile['|'.join[terms]]
>>> print[pat1.pattern]
a_42|\[a\^b\]|2\|3
>>> print[pat2.pattern]
a_42|[a^b]|2|3
>>> s = 'ba_423 [a^b]c 2|3 a^b'
>>> pat1.sub['X', s]
'bX3 Xc X a^b'
>>> pat2.sub['X', s]
'bXX [a^b]c X|X a^b'
Trình tự thoát
Một số ký tự như tab và newline có thể được thể hiện bằng các chuỗi thoát tương ứng là
>>> expr = '[a^b]'
# print used here to show results similar to raw string
>>> print[re.escape[expr]]
\[a\^b\]
# replace only at end of string
>>> eqn = 'f*[a^b] - 3*[a^b]'
>>> re.sub[re.escape[expr] + r'\Z', 'c', eqn]
'f*[a^b] - 3*c'
0 và >>> expr = '[a^b]'
# print used here to show results similar to raw string
>>> print[re.escape[expr]]
\[a\^b\]
# replace only at end of string
>>> eqn = 'f*[a^b] - 3*[a^b]'
>>> re.sub[re.escape[expr] + r'\Z', 'c', eqn]
'f*[a^b] - 3*c'
1. Đây là tương tự như cách chúng được đối xử trong các chữ viết bình thường. Tuy nhiên, >>> expr = '[a^b]'
# print used here to show results similar to raw string
>>> print[re.escape[expr]]
\[a\^b\]
# replace only at end of string
>>> eqn = 'f*[a^b] - 3*[a^b]'
>>> re.sub[re.escape[expr] + r'\Z', 'c', eqn]
'f*[a^b] - 3*c'
2 là dành cho các ranh giới từ như đã thấy trước đó, trong khi nó là viết tắt của ký tự backspace trong các chữ viết thông thường.Danh sách đầy đủ được đề cập ở cuối Docs.Python: Phần cú pháp biểu thức chính quy là
>>> expr = '[a^b]'
# print used here to show results similar to raw string
>>> print[re.escape[expr]]
\[a\^b\]
# replace only at end of string
>>> eqn = 'f*[a^b] - 3*[a^b]'
>>> re.sub[re.escape[expr] + r'\Z', 'c', eqn]
'f*[a^b] - 3*c'
3. Đọc tài liệu để biết chi tiết cũng như cách nó khác nhau đối với dữ liệu byte.>>> re.sub[r'\t', ':', 'a\tb\tc']
'a:b:c'
>>> re.sub[r'\n', ' ', '1\n2\n3']
'1 2 3'
Nếu một chuỗi thoát không được xác định, bạn sẽ gặp lỗi.
>>> re.search[r'\e', 'hello']
re.error: bad escape \e at position 0
Bạn cũng có thể đại diện cho một ký tự bằng cách sử dụng lối thoát thập lục phân của định dạng
>>> expr = '[a^b]'
# print used here to show results similar to raw string
>>> print[re.escape[expr]]
\[a\^b\]
# replace only at end of string
>>> eqn = 'f*[a^b] - 3*[a^b]'
>>> re.sub[re.escape[expr] + r'\Z', 'c', eqn]
'f*[a^b] - 3*c'
4 trong đó >>> expr = '[a^b]'
# print used here to show results similar to raw string
>>> print[re.escape[expr]]
\[a\^b\]
# replace only at end of string
>>> eqn = 'f*[a^b] - 3*[a^b]'
>>> re.sub[re.escape[expr] + r'\Z', 'c', eqn]
'f*[a^b] - 3*c'
5 chính xác là hai ký tự thập lục phân. Nếu bạn đại diện cho một metacharacter sử dụng Escapes, nó sẽ được đối xử theo nghĩa đen thay vì tính năng metacharacter của nó.# \x20 is space character
>>> re.sub[r'\x20', '', 'h e l l o']
'hello'
# \x7c is '|' character
>>> re.sub[r'2\x7c3', '5', '12|30']
'150'
>>> re.sub[r'2|3', '5', '12|30']
'15|50'
Xem bảng mã ASCII để biết một trò gian lận tiện dụng với tất cả các ký tự ASCII và đại diện thập lục phân của họ.
Escapes Octal sẽ được thảo luận trong phần Backreference. Phần Codepoint và Unicode Escapes sẽ thảo luận về Escapes cho các ký tự Unicode bằng cách sử dụng
>>> expr = '[a^b]'
# print used here to show results similar to raw string
>>> print[re.escape[expr]]
\[a\^b\]
# replace only at end of string
>>> eqn = 'f*[a^b] - 3*[a^b]'
>>> re.sub[re.escape[expr] + r'\Z', 'c', eqn]
'f*[a^b] - 3*c'
6 và >>> expr = '[a^b]'
# print used here to show results similar to raw string
>>> print[re.escape[expr]]
\[a\^b\]
# replace only at end of string
>>> eqn = 'f*[a^b] - 3*[a^b]'
>>> re.sub[re.escape[expr] + r'\Z', 'c', eqn]
'f*[a^b] - 3*c'
7.Cheatsheet và tóm tắt
4 | tiền tố metacharacters với 4 để phù hợp với chúng theo nghĩa đen |
6 | phù hợp với 4 theo nghĩa đen |
7 | tự động thoát khỏi tất cả các metacharacters |
Ví dụ: 3 | |
0 | Trình tự thoát như những người được hỗ trợ trong chuỗi chữ |
2 | Ranh giới từ trong re nhưng không gian ngược trong chuỗi chữ |
6 | Escapes không xác định sẽ dẫn đến lỗi |
4 | đại diện cho một ký tự bằng cách sử dụng giá trị thập lục phân |
8 | sẽ phù hợp 9 theo nghĩa đen |
Chương ngắn này đã thảo luận về cách phù hợp với Metacharacters theo nghĩa đen.
>>> eqn = 'f*[a^b] - 3*[a^b]'
# straightforward search and replace, no need RE shenanigans
>>> eqn.replace['[a^b]', 'c']
'f*c - 3*c'
7 giúp nếu bạn đang sử dụng các chuỗi đầu vào có nguồn gốc từ nơi khác để xây dựng RE cuối cùng. Bạn cũng đã thấy cách sử dụng trình tự thoát để thể hiện các ký tự và cách chúng khác với các chữ cái bình thường.Bài tập
a] Biến đổi các chuỗi đầu vào đã cho thành đầu ra dự kiến sử dụng cùng một logic trên cả hai chuỗi. Transform the given input strings to the expected output using same logic on both strings.
>>> str1 = '[9-2]*5+qty/3'
>>> str2 = '[qty+4]/2-[9-2]*5+pq/4'
##### add your solution here for str1
'35+qty/3'
##### add your solution here for str2
'[qty+4]/2-35+pq/4'
b] Thay thế
1 bằng >>> re.sub[r'\t', ':', 'a\tb\tc']
'a:b:c'
>>> re.sub[r'\n', ' ', '1\n2\n3']
'1 2 3'
2 chỉ khi bắt đầu hoặc kết thúc chuỗi đầu vào đã cho. Replace >>> re.sub[r'\t', ':', 'a\tb\tc']
'a:b:c'
>>> re.sub[r'\n', ' ', '1\n2\n3']
'1 2 3'
>>> re.sub[r'\t', ':', 'a\tb\tc']
'a:b:c'
>>> re.sub[r'\n', ' ', '1\n2\n3']
'1 2 3'
1 with >>> re.sub[r'\t', ':', 'a\tb\tc']
'a:b:c'
>>> re.sub[r'\n', ' ', '1\n2\n3']
'1 2 3'
2 only at the start or end of given input strings.>>> s1 = r'2.3/[4]\|6 foo 5.3-[4]\|'
>>> s2 = r'[4]\|42 - [4]\|3'
>>> s3 = 'two - [4]\\|\n'
>>> pat = re.compile[] ##### add your solution here
>>> pat.sub['2', s1]
'2.3/[4]\\|6 foo 5.3-2'
>>> pat.sub['2', s2]
'242 - [4]\\|3'
>>> pat.sub['2', s3]
'two - [4]\\|\n'
c] Thay thế bất kỳ phần tử phù hợp nào từ danh sách
3 bằng >>> re.sub[r'\t', ':', 'a\tb\tc']
'a:b:c'
>>> re.sub[r'\n', ' ', '1\n2\n3']
'1 2 3'
4 cho các chuỗi đầu vào. Phù hợp với các yếu tố từ >>> re.sub[r'\t', ':', 'a\tb\tc']
'a:b:c'
>>> re.sub[r'\n', ' ', '1\n2\n3']
'1 2 3'
3 theo nghĩa đen. Giả sử không có hai yếu tố của >>> re.sub[r'\t', ':', 'a\tb\tc']
'a:b:c'
>>> re.sub[r'\n', ' ', '1\n2\n3']
'1 2 3'
3 sẽ dẫn đến bất kỳ xung đột phù hợp nào. Replace any matching element from the list >>> re.sub[r'\t', ':', 'a\tb\tc']
'a:b:c'
>>> re.sub[r'\n', ' ', '1\n2\n3']
'1 2 3'
>>> re.sub[r'\t', ':', 'a\tb\tc']
'a:b:c'
>>> re.sub[r'\n', ' ', '1\n2\n3']
'1 2 3'
3 with >>> re.sub[r'\t', ':', 'a\tb\tc']
'a:b:c'
>>> re.sub[r'\n', ' ', '1\n2\n3']
'1 2 3'
4 for given the input strings. Match the elements from >>> re.sub[r'\t', ':', 'a\tb\tc']
'a:b:c'
>>> re.sub[r'\n', ' ', '1\n2\n3']
'1 2 3'
3 literally. Assume no two elements of >>> re.sub[r'\t', ':', 'a\tb\tc']
'a:b:c'
>>> re.sub[r'\n', ' ', '1\n2\n3']
'1 2 3'
3 will result in any matching conflict.>>> items = ['a.b', '3+n', r'x\y\z', 'qty||price', '{n}']
>>> pat = re.compile[] ##### add your solution here
>>> pat.sub['X', '0a.bcd']
'0Xcd'
>>> pat.sub['X', 'E{n}AMPLE']
'EXAMPLE'
>>> pat.sub['X', r'43+n2 ax\y\ze']
'4X2 aXe'
d] Thay thế ký tự backspace
2 bằng một ký tự không gian cho chuỗi đầu vào đã cho. Replace backspace
character >>> expr = '[a^b]'
# print used here to show results similar to raw string
>>> print[re.escape[expr]]
\[a\^b\]
# replace only at end of string
>>> eqn = 'f*[a^b] - 3*[a^b]'
>>> re.sub[re.escape[expr] + r'\Z', 'c', eqn]
'f*[a^b] - 3*c'
>>> expr = '[a^b]'
# print used here to show results similar to raw string
>>> print[re.escape[expr]]
\[a\^b\]
# replace only at end of string
>>> eqn = 'f*[a^b] - 3*[a^b]'
>>> re.sub[re.escape[expr] + r'\Z', 'c', eqn]
'f*[a^b] - 3*c'
2 with a single space character for the given input string.>>> eqn = 'f*[a^b] - 3*[a^b]'
# straightforward search and replace, no need RE shenanigans
>>> eqn.replace['[a^b]', 'c']
'f*c - 3*c'
0e] Thay thế tất cả các lần xuất hiện của
6 bằng # iterable of strings, assume alternation precedence sorting isn't needed
>>> terms = ['a_42', '[a^b]', '2|3']
# using 're.escape' and 'join' to construct the pattern
>>> pat1 = re.compile['|'.join[re.escape[s] for s in terms]]
# using only 'join' to construct the pattern
>>> pat2 = re.compile['|'.join[terms]]
>>> print[pat1.pattern]
a_42|\[a\^b\]|2\|3
>>> print[pat2.pattern]
a_42|[a^b]|2|3
>>> s = 'ba_423 [a^b]c 2|3 a^b'
>>> pat1.sub['X', s]
'bX3 Xc X a^b'
>>> pat2.sub['X', s]
'bXX [a^b]c X|X a^b'
9. Replace all occurrences of >>> re.sub[r'\t', ':', 'a\tb\tc']
'a:b:c'
>>> re.sub[r'\n', ' ', '1\n2\n3']
'1 2 3'
# iterable of strings, assume alternation precedence sorting isn't needed
>>> terms = ['a_42', '[a^b]', '2|3']
# using 're.escape' and 'join' to construct the pattern
>>> pat1 = re.compile['|'.join[re.escape[s] for s in terms]]
# using only 'join' to construct the pattern
>>> pat2 = re.compile['|'.join[terms]]
>>> print[pat1.pattern]
a_42|\[a\^b\]|2\|3
>>> print[pat2.pattern]
a_42|[a^b]|2|3
>>> s = 'ba_423 [a^b]c 2|3 a^b'
>>> pat1.sub['X', s]
'bX3 Xc X a^b'
>>> pat2.sub['X', s]
'bXX [a^b]c X|X a^b'
6 with >>> re.sub[r'\t', ':', 'a\tb\tc']
'a:b:c'
>>> re.sub[r'\n', ' ', '1\n2\n3']
'1 2 3'
9.>>> eqn = 'f*[a^b] - 3*[a^b]'
# straightforward search and replace, no need RE shenanigans
>>> eqn.replace['[a^b]', 'c']
'f*c - 3*c'
1f] Thay thế bất kỳ mục phù hợp nào từ danh sách
0 bằng >>> re.search[r'\e', 'hello']
re.error: bad escape \e at position 0
4 cho chuỗi >>> re.sub[r'\t', ':', 'a\tb\tc']
'a:b:c'
>>> re.sub[r'\n', ' ', '1\n2\n3']
'1 2 3'
2. Kết hợp các mục từ >>> re.search[r'\e', 'hello']
re.error: bad escape \e at position 0
0 theo nghĩa đen. Replace any matching item from the list >>> re.search[r'\e', 'hello']
re.error: bad escape \e at position 0
>>> re.search[r'\e', 'hello']
re.error: bad escape \e at position 0
0 with >>> re.sub[r'\t', ':', 'a\tb\tc']
'a:b:c'
>>> re.sub[r'\n', ' ', '1\n2\n3']
'1 2 3'
4 for given the string >>> re.search[r'\e', 'hello']
re.error: bad escape \e at position 0
2. Match the items from >>> re.search[r'\e', 'hello']
re.error: bad escape \e at position 0
0 literally.>>> eqn = 'f*[a^b] - 3*[a^b]'
# straightforward search and replace, no need RE shenanigans
>>> eqn.replace['[a^b]', 'c']
'f*c - 3*c'
2