Trong Python, các ký tự thoát luôn đứng trước dấu gạch chéo ngược [\]. Điều này hữu ích cho việc định dạng nhưng nếu chúng ta muốn sử dụng dấu gạch chéo ngược trong chuỗi thì sao?
Hãy xem một ví dụ để hiểu rõ hơn
print[“Backslash: \\”]
print[“New Line char: \\n”]
Hai dòng này sẽ cho đầu ra sau
đầu ra
Điều này có thể "khó đọc". Có một sự thay thế cho điều này.
Chúng ta có thể sử dụng một chuỗi thô sẽ xử lý dấu gạch chéo ngược theo nghĩa đen. Bằng cách nào?
Dung dịch. Đơn giản chỉ cần đặt một "r" hoặc "R" trước chuỗi
Xem đoạn mã dưới đây
print[r”This is a backslash: \ and a new line: \n”]
đầu ra
Tôi hy vọng bạn hiểu sự khác biệt khi sử dụng chuỗi thô. Bạn đã?
Sự khác biệt là gì?
Hãy xem thêm. Đầu tiên, tại sao chúng ta sử dụng chuỗi thô trong python?
Chuỗi thô Python coi dấu gạch chéo ngược [\] là ký tự chữ.
Thật hữu ích khi chúng ta muốn có một chuỗi chứa dấu gạch chéo ngược [\] và không muốn nó được coi là một ký tự thoát.
Hãy xem một ví dụ khác để làm cho nó rõ ràng
my_string = ‘Hello\nWorld’
print[my_string]
đầu ra
Bây giờ, hãy xem chuỗi thô hoạt động như thế nào
my_raw_str =r’Hello\nWorld’
print[my_raw_str]
đầu ra
CHUỖI RAW KHÔNG HỢP LỆ
Không phải tất cả các chuỗi thô đều hợp lệ. Một chuỗi thô chỉ chứa một dấu gạch chéo ngược không hợp lệ. Tương tự, các chuỗi thô có số lẻ dấu gạch chéo ngược ở cuối cũng không hợp lệ
Xem ví dụ bên dưới
raw_str_one = r’\’
raw_str_two = r’xyz\’
raw_str_tree = r’xyz\\\’
Bây giờ, tôi nghĩ bạn đã hiểu rõ về cách sử dụng chuỗi thô trong python.
Điều này thật tuyệt nhưng việc sử dụng thực sự đi kèm với “Biểu thức chính quy[REGEX]”. Regex thường sử dụng dấu gạch chéo ngược [\] để biểu thị các ký tự đặc biệt và nó trở nên lộn xộn nếu không có chuỗi thô.
Biểu thức chính quy thường chứa rất nhiều dấu gạch chéo ngược [\]. Khi sử dụng mô-đun “re” của Python, các biểu thức chính quy được biểu diễn dưới dạng chuỗi. Vì vậy, giống như tất cả các chuỗi có nhiều dấu gạch chéo ngược, chúng dễ đọc hơn khi được viết ở dạng chữ thô
Chuỗi thô là tuyệt vời cho regex
Hãy hiểu nó với các biểu thức regex cơ bản
#import Python's regular expressions module
import re#compile a regular expression.. note the raw string literal that doesn't have its backslashes escaped p = re.compile[r'[Ee][\+|-]?[0-9]+']
Trong ví dụ trên, nếu chúng ta không viết các biểu thức chính quy dưới dạng một chuỗi thô, chúng ta sẽ phải thoát khỏi bất kỳ dấu gạch chéo ngược nào, điều này sẽ làm cho chuỗi thậm chí còn khó đọc hơn hiện tại.
Phương thức tĩnh
raw_str_one = r’\’
raw_str_two = r’xyz\’
raw_str_tree = r’xyz\\\’
0 là một chức năng thẻ của mẫu chữ. Điều này tương tự như tiền tố raw_str_one = r’\’
raw_str_two = r’xyz\’
raw_str_tree = r’xyz\\\’
1 trong Python hoặc tiền tố raw_str_one = r’\’
raw_str_two = r’xyz\’
raw_str_tree = r’xyz\\\’
2 trong C# cho chuỗi ký tự. Nó được sử dụng để lấy dạng chuỗi thô của mẫu chữ — nghĩa là, thay thế [e. g. String.raw`Hi\n${2 + 3}!`;
// 'Hi\\n5!', the character after 'Hi'
// is not a newline character,
// '\' and 'n' are two characters.
String.raw`Hi\u000A!`;
// 'Hi\\u000A!', same here, this time we will get the
// \, u, 0, 0, 0, A, 6 characters.
// All kinds of escape characters will be ineffective
// and backslashes will be present in the output string.
// You can confirm this by checking the .length property
// of the string.
const name = "Bob";
String.raw`Hi\n${name}!`;
// 'Hi\\nBob!', substitutions are processed.
String.raw`Hi \${name}!`;
// 'Hi \\${name}!', the dollar sign is escaped; there's no interpolation.
0] được xử lý, nhưng các chuỗi thoát [e. g. String.raw`Hi\n${2 + 3}!`;
// 'Hi\\n5!', the character after 'Hi'
// is not a newline character,
// '\' and 'n' are two characters.
String.raw`Hi\u000A!`;
// 'Hi\\u000A!', same here, this time we will get the
// \, u, 0, 0, 0, A, 6 characters.
// All kinds of escape characters will be ineffective
// and backslashes will be present in the output string.
// You can confirm this by checking the .length property
// of the string.
const name = "Bob";
String.raw`Hi\n${name}!`;
// 'Hi\\nBob!', substitutions are processed.
String.raw`Hi \${name}!`;
// 'Hi \\${name}!', the dollar sign is escaped; there's no interpolation.
1] khôngThử nó
cú pháp
________số 8
Thông số
String.raw`Hi\n${2 + 3}!`;
// 'Hi\\n5!', the character after 'Hi'
// is not a newline character,
// '\' and 'n' are two characters.
String.raw`Hi\u000A!`;
// 'Hi\\u000A!', same here, this time we will get the
// \, u, 0, 0, 0, A, 6 characters.
// All kinds of escape characters will be ineffective
// and backslashes will be present in the output string.
// You can confirm this by checking the .length property
// of the string.
const name = "Bob";
String.raw`Hi\n${name}!`;
// 'Hi\\nBob!', substitutions are processed.
String.raw`Hi \${name}!`;
// 'Hi \\${name}!', the dollar sign is escaped; there's no interpolation.
2Đối tượng mảng chữ mẫu được định dạng tốt, như
String.raw`Hi\n${2 + 3}!`;
// 'Hi\\n5!', the character after 'Hi'
// is not a newline character,
// '\' and 'n' are two characters.
String.raw`Hi\u000A!`;
// 'Hi\\u000A!', same here, this time we will get the
// \, u, 0, 0, 0, A, 6 characters.
// All kinds of escape characters will be ineffective
// and backslashes will be present in the output string.
// You can confirm this by checking the .length property
// of the string.
const name = "Bob";
String.raw`Hi\n${name}!`;
// 'Hi\\nBob!', substitutions are processed.
String.raw`Hi \${name}!`;
// 'Hi \\${name}!', the dollar sign is escaped; there's no interpolation.
3. Phải là một đối tượng có thuộc tính String.raw`Hi\n${2 + 3}!`;
// 'Hi\\n5!', the character after 'Hi'
// is not a newline character,
// '\' and 'n' are two characters.
String.raw`Hi\u000A!`;
// 'Hi\\u000A!', same here, this time we will get the
// \, u, 0, 0, 0, A, 6 characters.
// All kinds of escape characters will be ineffective
// and backslashes will be present in the output string.
// You can confirm this by checking the .length property
// of the string.
const name = "Bob";
String.raw`Hi\n${name}!`;
// 'Hi\\nBob!', substitutions are processed.
String.raw`Hi \${name}!`;
// 'Hi \\${name}!', the dollar sign is escaped; there's no interpolation.
4 có giá trị là một đối tượng chuỗi giống như mảngString.raw`Hi\n${2 + 3}!`;
// 'Hi\\n5!', the character after 'Hi'
// is not a newline character,
// '\' and 'n' are two characters.
String.raw`Hi\u000A!`;
// 'Hi\\u000A!', same here, this time we will get the
// \, u, 0, 0, 0, A, 6 characters.
// All kinds of escape characters will be ineffective
// and backslashes will be present in the output string.
// You can confirm this by checking the .length property
// of the string.
const name = "Bob";
String.raw`Hi\n${name}!`;
// 'Hi\\nBob!', substitutions are processed.
String.raw`Hi \${name}!`;
// 'Hi \\${name}!', the dollar sign is escaped; there's no interpolation.
5Chứa các giá trị thay thế
String.raw`Hi\n${2 + 3}!`;
// 'Hi\\n5!', the character after 'Hi'
// is not a newline character,
// '\' and 'n' are two characters.
String.raw`Hi\u000A!`;
// 'Hi\\u000A!', same here, this time we will get the
// \, u, 0, 0, 0, A, 6 characters.
// All kinds of escape characters will be ineffective
// and backslashes will be present in the output string.
// You can confirm this by checking the .length property
// of the string.
const name = "Bob";
String.raw`Hi\n${name}!`;
// 'Hi\\nBob!', substitutions are processed.
String.raw`Hi \${name}!`;
// 'Hi \\${name}!', the dollar sign is escaped; there's no interpolation.
6Một mẫu chữ, tùy chọn với sự thay thế [_______67]
Giá trị trả về
Dạng chuỗi thô của một mẫu chữ nhất định
ngoại lệ
String.raw`Hi\n${2 + 3}!`;
// 'Hi\\n5!', the character after 'Hi'
// is not a newline character,
// '\' and 'n' are two characters.
String.raw`Hi\u000A!`;
// 'Hi\\u000A!', same here, this time we will get the
// \, u, 0, 0, 0, A, 6 characters.
// All kinds of escape characters will be ineffective
// and backslashes will be present in the output string.
// You can confirm this by checking the .length property
// of the string.
const name = "Bob";
String.raw`Hi\n${name}!`;
// 'Hi\\nBob!', substitutions are processed.
String.raw`Hi \${name}!`;
// 'Hi \\${name}!', the dollar sign is escaped; there's no interpolation.
8Bị ném nếu đối số đầu tiên không có thuộc tính
String.raw`Hi\n${2 + 3}!`;
// 'Hi\\n5!', the character after 'Hi'
// is not a newline character,
// '\' and 'n' are two characters.
String.raw`Hi\u000A!`;
// 'Hi\\u000A!', same here, this time we will get the
// \, u, 0, 0, 0, A, 6 characters.
// All kinds of escape characters will be ineffective
// and backslashes will be present in the output string.
// You can confirm this by checking the .length property
// of the string.
const name = "Bob";
String.raw`Hi\n${name}!`;
// 'Hi\\nBob!', substitutions are processed.
String.raw`Hi \${name}!`;
// 'Hi \\${name}!', the dollar sign is escaped; there's no interpolation.
4 hoặc thuộc tính String.raw`Hi\n${2 + 3}!`;
// 'Hi\\n5!', the character after 'Hi'
// is not a newline character,
// '\' and 'n' are two characters.
String.raw`Hi\u000A!`;
// 'Hi\\u000A!', same here, this time we will get the
// \, u, 0, 0, 0, A, 6 characters.
// All kinds of escape characters will be ineffective
// and backslashes will be present in the output string.
// You can confirm this by checking the .length property
// of the string.
const name = "Bob";
String.raw`Hi\n${name}!`;
// 'Hi\\nBob!', substitutions are processed.
String.raw`Hi \${name}!`;
// 'Hi \\${name}!', the dollar sign is escaped; there's no interpolation.
4 là print[r”This is a backslash: \ and a new line: \n”]
81 hoặc print[r”This is a backslash: \ and a new line: \n”]
82Sự miêu tả
Trong hầu hết các trường hợp,
raw_str_one = r’\’
raw_str_two = r’xyz\’
raw_str_tree = r’xyz\\\’
0 được sử dụng với chữ mẫu. Cú pháp đầu tiên được đề cập ở trên hiếm khi được sử dụng, bởi vì công cụ JavaScript sẽ gọi cú pháp này với các đối số thích hợp cho bạn, [giống như với các hàm thẻ khác]raw_str_one = r’\’
raw_str_two = r’xyz\’
raw_str_tree = r’xyz\\\’
0 là thẻ chữ mẫu tích hợp duy nhất. Nó có ngữ nghĩa gần với một nghĩa đen không được đánh dấu vì nó nối tất cả các đối số và trả về một chuỗi. Bạn thậm chí có thể triển khai lại nó bằng mã JavaScript bình thườngCảnh báo. Bạn không nên sử dụng trực tiếp
print[r”This is a backslash: \ and a new line: \n”]
85 làm thẻ "danh tính". Xem Xây dựng thẻ nhận dạng để biết cách triển khai điều nàyNếu
raw_str_one = r’\’
raw_str_two = r’xyz\’
raw_str_tree = r’xyz\\\’
0 được gọi với một đối tượng có thuộc tính String.raw`Hi\n${2 + 3}!`;
// 'Hi\\n5!', the character after 'Hi'
// is not a newline character,
// '\' and 'n' are two characters.
String.raw`Hi\u000A!`;
// 'Hi\\u000A!', same here, this time we will get the
// \, u, 0, 0, 0, A, 6 characters.
// All kinds of escape characters will be ineffective
// and backslashes will be present in the output string.
// You can confirm this by checking the .length property
// of the string.
const name = "Bob";
String.raw`Hi\n${name}!`;
// 'Hi\\nBob!', substitutions are processed.
String.raw`Hi \${name}!`;
// 'Hi \\${name}!', the dollar sign is escaped; there's no interpolation.
4 không có thuộc tính print[r”This is a backslash: \ and a new line: \n”]
88 hoặc một print[r”This is a backslash: \ and a new line: \n”]
88 không dương, nó sẽ trả về một chuỗi rỗng
my_string = ‘Hello\nWorld’
print[my_string]
40. Nếu
my_string = ‘Hello\nWorld’
print[my_string]
41 [tôi. e. không có đủ sự thay thế để lấp đầy các chỗ dành sẵn — điều này không thể xảy ra trong một mẫu được gắn thẻ được định dạng tốt theo nghĩa đen], phần còn lại của các chỗ dành sẵn chứa đầy các chuỗi trốngví dụ
Sử dụng Chuỗi. thô[]
String.raw`Hi\n${2 + 3}!`;
// 'Hi\\n5!', the character after 'Hi'
// is not a newline character,
// '\' and 'n' are two characters.
String.raw`Hi\u000A!`;
// 'Hi\\u000A!', same here, this time we will get the
// \, u, 0, 0, 0, A, 6 characters.
// All kinds of escape characters will be ineffective
// and backslashes will be present in the output string.
// You can confirm this by checking the .length property
// of the string.
const name = "Bob";
String.raw`Hi\n${name}!`;
// 'Hi\\nBob!', substitutions are processed.
String.raw`Hi \${name}!`;
// 'Hi \\${name}!', the dollar sign is escaped; there's no interpolation.
Xây dựng thẻ nhận dạng
Nhiều công cụ dành sự đối xử đặc biệt cho các chữ được gắn thẻ bằng một tên cụ thể
print[r”This is a backslash: \ and a new line: \n”]
8Người ta có thể triển khai thẻ
my_string = ‘Hello\nWorld’
print[my_string]
42 một cách ngây thơ như
my_string = ‘Hello\nWorld’
print[my_string]
4Điều này, trên thực tế, hoạt động cho trường hợp trên. Tuy nhiên, vì
print[r”This is a backslash: \ and a new line: \n”]
85 sẽ nối các chuỗi ký tự thô thay vì chuỗi "đã nấu chín", các chuỗi thoát sẽ không được xử lýmy_raw_str =r’Hello\nWorld’
print[my_raw_str]
1Đây có thể không phải là điều bạn muốn đối với thẻ "danh tính thực", trong đó thẻ hoàn toàn là để đánh dấu và không thay đổi giá trị của chữ. Trong trường hợp này, bạn có thể tạo thẻ tùy chỉnh và chuyển thẻ "đã nấu chín" [tôi. e. trình tự thoát được xử lý] mảng chữ thành
print[r”This is a backslash: \ and a new line: \n”]
85, giả vờ rằng chúng là chuỗi thômy_raw_str =r’Hello\nWorld’
print[my_raw_str]
3Lưu ý rằng đối số đầu tiên là một đối tượng có thuộc tính
String.raw`Hi\n${2 + 3}!`;
// 'Hi\\n5!', the character after 'Hi'
// is not a newline character,
// '\' and 'n' are two characters.
String.raw`Hi\u000A!`;
// 'Hi\\u000A!', same here, this time we will get the
// \, u, 0, 0, 0, A, 6 characters.
// All kinds of escape characters will be ineffective
// and backslashes will be present in the output string.
// You can confirm this by checking the .length property
// of the string.
const name = "Bob";
String.raw`Hi\n${name}!`;
// 'Hi\\nBob!', substitutions are processed.
String.raw`Hi \${name}!`;
// 'Hi \\${name}!', the dollar sign is escaped; there's no interpolation.
4, có giá trị là một đối tượng giống như mảng [với thuộc tính print[r”This is a backslash: \ and a new line: \n”]
88 và các chỉ mục số nguyên] đại diện cho các chuỗi được phân tách trong mẫu chữ. Phần còn lại của các đối số là sự thay thế. Vì giá trị String.raw`Hi\n${2 + 3}!`;
// 'Hi\\n5!', the character after 'Hi'
// is not a newline character,
// '\' and 'n' are two characters.
String.raw`Hi\u000A!`;
// 'Hi\\u000A!', same here, this time we will get the
// \, u, 0, 0, 0, A, 6 characters.
// All kinds of escape characters will be ineffective
// and backslashes will be present in the output string.
// You can confirm this by checking the .length property
// of the string.
const name = "Bob";
String.raw`Hi\n${name}!`;
// 'Hi\\nBob!', substitutions are processed.
String.raw`Hi \${name}!`;
// 'Hi \\${name}!', the dollar sign is escaped; there's no interpolation.
4 có thể là bất kỳ đối tượng giống như mảng nào, nên nó thậm chí có thể là một chuỗi. Ví dụ:
my_string = ‘Hello\nWorld’
print[my_string]
48 được coi là
my_string = ‘Hello\nWorld’
print[my_string]
49. Sau đây là tương đương với my_raw_str =r’Hello\nWorld’
print[my_raw_str]
10