Hướng dẫn how do you write special characters in a regular expression in python? - làm thế nào để bạn viết các ký tự đặc biệt trong một biểu thức chính quy trong python?

Regular expressions are a strange animal. Many students find them difficult to understand – do you?

Regex Special Characters - Examples in Python Re

I realized that a major reason for this is simply that they don’t understand the special regex characters. To put it differently: understand the special characters and everything else in the regex space will come much easier to you.

Related article: Python Regex Superpower – The Ultimate Guide

Do you want to master the regex superpower? 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.

Regular expressions are built from characters. There are two types of characters: literal characters and special characters.

Literal Characters

Let’s start with the absolute first thing you need to know with regular expressions: a regular expression (short: regex) searches for a given pattern in a given string.

What’s a pattern? In its most basic form, a pattern can be a literal character. So the literal characters 'a', 'b', and 'c' are all valid regex patterns.

For example, you can search for the regex pattern 'a' in the string 'hello world' but it won’t find a match. You can also search for the pattern 'a' in the string

.      – -->     \.
*      – -->     \*
?      – -->     \?
+      – -->     \+
^      – -->     \^
$      – -->     \$
|      – -->     \|
0 and there is a match: the second last character in the string.

Based on the simple insight that a literal character is a valid regex pattern, you’ll find that a combination of literal characters is also a valid regex pattern. For example, the regex pattern

.      – -->     \.
*      – -->     \*
?      – -->     \?
+      – -->     \+
^      – -->     \^
$      – -->     \$
|      – -->     \|
1 matches the last two characters in the string
.      – -->     \.
*      – -->     \*
?      – -->     \?
+      – -->     \+
^      – -->     \^
$      – -->     \$
|      – -->     \|
0.

Summary: 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.

However, the power of regular expressions come from their abstraction capability. Instead of writing the character set

.      – -->     \.
*      – -->     \*
?      – -->     \?
+      – -->     \+
^      – -->     \^
$      – -->     \$
|      – -->     \|
3, you’d write
.      – -->     \.
*      – -->     \*
?      – -->     \?
+      – -->     \+
^      – -->     \^
$      – -->     \$
|      – -->     \|
4 or even
.      – -->     \.
*      – -->     \*
?      – -->     \?
+      – -->     \+
^      – -->     \^
$      – -->     \$
|      – -->     \|
5. The latter is a special regex character—and pros know them by heart. In fact, regex experts seldomly match literal characters. In most cases, they use more advanced constructs or special characters for various reasons such as brevity, expressiveness, or generality.

So what are the special characters you can use in your regex patterns?

Let’s have a look at the following table that contains all special characters in Python’s

.      – -->     \.
*      – -->     \*
?      – -->     \?
+      – -->     \+
^      – -->     \^
$      – -->     \$
|      – -->     \|
6 package for regular expression processing.

Special CharacterMeaning
.      – -->     \.
*      – -->     \*
?      – -->     \?
+      – -->     \+
^      – -->     \^
$      – -->     \$
|      – -->     \|
7
The newline symbol is not a special symbol particular to regex only, it’s actually one of the most widely-used, standard characters. However, you’ll see the newline character so often that I just couldn’t write this list without including it. For example, the regex
.      – -->     \.
*      – -->     \*
?      – -->     \?
+      – -->     \+
^      – -->     \^
$      – -->     \$
|      – -->     \|
8 matches a string where the string
.      – -->     \.
*      – -->     \*
?      – -->     \?
+      – -->     \+
^      – -->     \^
$      – -->     \$
|      – -->     \|
9 is placed in one line and the string
|^&+-%*/=!>
0 is placed into the second line. 
|^&+-%*/=!>
1
The tabular character is, like the newline character, not a “regex-specific” symbol. It just encodes the tabular space
|^&+-%*/=!>
2 which is different to a sequence of whitespaces (even if it doesn’t look different over here). For example, the regex
|^&+-%*/=!>
3 matches the string that consists of
.      – -->     \.
*      – -->     \*
?      – -->     \?
+      – -->     \+
^      – -->     \^
$      – -->     \$
|      – -->     \|
9 in the first line and
|^&+-%*/=!>
5 in the second line (with a leading tab character).
|^&+-%*/=!>
6
The whitespace character is, in contrast to the newline character, a special symbol of the regex libraries. You’ll find it in many other programming languages, too. The problem is that you often don’t know which type of whitespace is used: tabular characters, simple whitespaces, or even newlines. The whitespace character
|^&+-%*/=!>
7 simply matches any of them. For example, the regex
|^&+-%*/=!>
8 matches the string
|^&+-%*/=!>
9, as well as 'hello world'.
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))
'''
['|']
['^']
['$']
['+']
['-']
['%']
['*']
['/']
['=']
['!']
'''
1
The whitespace-negation character matches everything that does not match
|^&+-%*/=!>
6.
.      – -->     \.
*      – -->     \*
?      – -->     \?
+      – -->     \+
^      – -->     \^
$      – -->     \$
|      – -->     \|
5
The word character regex simplifies text processing significantly. It represents the class of all characters used in typical words (
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))
'''
['|']
['^']
['$']
['+']
['-']
['%']
['*']
['/']
['=']
['!']
'''
4,
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))
'''
['|']
['^']
['$']
['+']
['-']
['%']
['*']
['/']
['=']
['!']
'''
5,
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))
'''
['|']
['^']
['$']
['+']
['-']
['%']
['*']
['/']
['=']
['!']
'''
6, and
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))
'''
['|']
['^']
['$']
['+']
['-']
['%']
['*']
['/']
['=']
['!']
'''
7). This simplifies the writing of complex regular expressions significantly. For example, the regex
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))
'''
['|']
['^']
['$']
['+']
['-']
['%']
['*']
['/']
['=']
['!']
'''
8 matches the strings
.      – -->     \.
*      – -->     \*
?      – -->     \?
+      – -->     \+
^      – -->     \^
$      – -->     \$
|      – -->     \|
9, 'a'0, 'a'1, and 'a'2. 
'a'3 The word-character-negation. It matches any character that is not a word character.
'a'4 The word boundary is also a special symbol used in many regex tools. You can use it to match,  as the name suggests, the boundary between the a word character (
.      – -->     \.
*      – -->     \*
?      – -->     \?
+      – -->     \+
^      – -->     \^
$      – -->     \$
|      – -->     \|
5) and a non-word ('a'3) character. But note that it matches only the empty string! You may ask: why does it exist if it doesn’t match any character? The reason is that it doesn’t “consume” the character right in front or right after a word. This way, you can search for whole words (or parts of words) and return only the word but not the delimiting characters that separate the word, e.g.,  from other words.
'a'7Ký tự chữ số khớp với tất cả các ký hiệu số trong khoảng từ 0 đến 9. Bạn có thể sử dụng nó để khớp các số nguyên với số chữ số tùy ý: regex 'a'8 khớp với số nguyên 'a'9, 'b'0, 'b'1 và 'b'2.digit character matches all numeric symbols between 0 and 9. You can use it to match integers with an arbitrary number of digits: the regex 'a'8 matches integer numbers 'a'9, 'b'0, 'b'1, and 'b'2.
'b'3Khớp với bất kỳ ký tự không chữ số. Đây là nghịch đảo của 'a'7 và nó tương đương với 'b'5.non-digit character. This is the inverse of 'a'7 and it’s equivalent to 'b'5.

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 'b'6 phù hợp với số lượng ký tự tùy ý theo sau là hậu tố 'b'7. Mẫu này có hai ký tự regex đặc biệt: DOT 'b'8 và toán tử Asterisk 'b'9. Bây giờ bạn sẽ tìm hiểu về những nhân vật meta đó:

Ký tự meta regex

Hã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:

Nhân vật metaNghĩa
'b'8Toán tử thẻ hoang dã (DOT) khớp với bất kỳ ký tự nào trong chuỗi ngoại trừ ký tự mới 'c'1. Ví dụ: Regex 'c'2 khớp với tất cả các từ có ba ký tự như 'c'3, 'c'4 và ________ 65. & nbsp; & nbsp;wild-card operator (dot) matches any character in a string except the newline character 'c'1. For example, the regex 'c'2 matches all words with three characters such as 'c'3, 'c'4, and 'c'5.  
'b'9Toán tử dấu hoa thị không hoặc không có nhiều lần xuất hiện tùy ý (bao gồm cả các lần xuất hiện bằng không) của Regex ngay trước đó. Ví dụ: Regex ‘Cat*, khớp với các chuỗi 'c'7, 'c'4, 'c'9, 'a'0 và 'a'1.zero-or-more asterisk operator matches an arbitrary number of occurrences (including zero occurrences) of the immediately preceding regex. For example, the regex ‘cat*’ matches the strings 'c'7, 'c'4, 'c'9, 'a'0, and 'a'1.
'a'2Toán tử không hoặc một khớp nối (như tên cho thấy) hoặc bằng không hoặc một lần xuất hiện của Regex ngay trước đó. Ví dụ: Regex ‘Cat? Khăn khớp với cả hai chuỗi 'a'3 và 'a'4 - nhưng không phải 'a'5, 'a'6 và ________ 77. & nbsp;zero-or-one operator matches (as the name suggests) either zero or one occurrences of the immediately preceding regex. For example, the regex ‘cat?’ matches both strings 'a'3 and 'a'4 — but not 'a'5, 'a'6, and 'a'7. 
'a'8Toán tử tại địa điểm một hoặc nhiều lần xuất hiện của Regex ngay trước đó. Ví dụ: Regex 'a'9 không khớp với chuỗi 'a'3 nhưng khớp với tất cả các chuỗi với ít nhất một ký tự dấu 'hello world'1 như 'a'4, 'a'5 và ________ 76. & nbsp;at-least-one operator matches one or more occurrences of the immediately preceding regex. For example, the regex 'a'9 does not match the string 'a'3 but matches all strings with at least one trailing character 'hello world'1 such as 'a'4, 'a'5, and 'a'6. 
'hello world'5Toán tử khởi động phù hợp với sự khởi đầu của một chuỗi. Ví dụ: Regex 'hello world'6 sẽ khớp với các chuỗi 'hello world'7 và 'hello world'8 nhưng không phải 'hello world'9 và 'a'0 trong đó ký tự 'a'1 không xảy ra khi bắt đầu chuỗi.start-of-string operator matches the beginning of a string. For example, the regex 'hello world'6 would match the strings 'hello world'7 and 'hello world'8 but not 'hello world'9 and 'a'0 where the character 'a'1 does not occur at the start of the string.
'a'2Toán tử cuối chuỗi khớp với phần cuối của chuỗi. Ví dụ: Regex 'a'3 sẽ khớp với các chuỗi 'a'4 và 'a'5 nhưng không phải là chuỗi 'hello world'7 và ________ 97. & nbsp;end-of-string operator matches the end of a string. For example, the regex 'a'3 would match the strings 'a'4 and 'a'5 but not the strings 'hello world'7 and 'a'7. 
'a'8Toán tử OR phù hợp với Regex A hoặc Regex B. Lưu ý rằng trực giác khá khác biệt so với cách giải thích tiêu chuẩn của toán tử hoặc người vận hành cũng có thể thỏa mãn cả hai điều kiện. Ví dụ: Regex 'a'9 khớp với các chuỗi
.      – -->     \.
*      – -->     \*
?      – -->     \?
+      – -->     \+
^      – -->     \^
$      – -->     \$
|      – -->     \|
00 và
.      – -->     \.
*      – -->     \*
?      – -->     \?
+      – -->     \+
^      – -->     \^
$      – -->     \$
|      – -->     \|
01. Nó sẽ có ý nghĩa khi cố gắng phù hợp với cả hai người cùng một lúc.OR operator matches either the regex A or the regex B. Note that the intuition is quite different from the standard interpretation of the or operator that can also satisfy both conditions. For example, the regex 'a'9 matches strings
.      – -->     \.
*      – -->     \*
?      – -->     \?
+      – -->     \+
^      – -->     \^
$      – -->     \$
|      – -->     \|
00 and
.      – -->     \.
*      – -->     \*
?      – -->     \?
+      – -->     \+
^      – -->     \^
$      – -->     \$
|      – -->     \|
01. It wouldn’t make sense to try to match both of them at the same time.
________ 102 & nbsp;Toán tử và đầu tiên khớp với Regex A và thứ hai là Regex B, trong chuỗi này. Chúng tôi đã thấy nó tầm thường trong Regex 'a'3 phù hợp với Regex
.      – -->     \.
*      – -->     \*
?      – -->     \?
+      – -->     \+
^      – -->     \^
$      – -->     \$
|      – -->     \|
04 đầu tiên và Regex thứ hai ________ 105. & nbsp;AND operator matches first the regex A and second the regex B, in this sequence. We’ve already seen it trivially in the regex 'a'3 that matches first regex
.      – -->     \.
*      – -->     \*
?      – -->     \?
+      – -->     \+
^      – -->     \^
$      – -->     \$
|      – -->     \|
04 and second regex
.      – -->     \.
*      – -->     \*
?      – -->     \?
+      – -->     \+
^      – -->     \^
$      – -->     \$
|      – -->     \|
05. 

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ử

.      – -->     \.
*      – -->     \*
?      – -->     \?
+      – -->     \+
^      – -->     \^
$      – -->     \$
|      – -->     \|
06 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

.      – -->     \.
*      – -->     \*
?      – -->     \?
+      – -->     \+
^      – -->     \^
$      – -->     \$
|      – -->     \|
7 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.

  • Trong lớp ký tự, bạn chỉ cần thoát khỏi biểu tượng trừ thay thế
    .      – -->     \.
    *      – -->     \*
    ?      – -->     \?
    +      – -->     \+
    ^      – -->     \^
    $      – -->     \$
    |      – -->     \|
    08 bằng
    .      – -->     \.
    *      – -->     \*
    ?      – -->     \?
    +      – -->     \+
    ^      – -->     \^
    $      – -->     \$
    |      – -->     \|
    09 vì điều này có ý nghĩa đặc biệt trong lớp ký tự (ký tự phạm vi phạm vi).
  • Bên ngoài lớp ký tự theo mẫu Regex bình thường, bạn chỉ cần thoát khỏi Regex Chars với ý nghĩa đặc biệt. Ở đâ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:
    .      – -->     \.
    *      – -->     \*
    ?      – -->     \?
    +      – -->     \+
    ^      – -->     \^
    $      – -->     \$
    |      – -->     \|
    10
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ừ đây

Bạ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

Hướng dẫn how do you write special characters in a regular expression in python? - làm thế nào để bạn viết các ký tự đặc biệt trong một biểu thức chính quy trong python?
Đợi đã, quên thoát một không gian. Wheeeeee [Taptaptap] Eeeeee. (nguồn)

Hướng dẫn how do you write special characters in a regular expression in python? - làm thế nào để bạn viết các ký tự đặc biệt trong một biểu thức chính quy trong python?

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.

Làm thế nào để bạn viết các ký tự đặc biệt trong Regex Python?

Thoát khỏi Char đặc biệt hoặc bắt đầu một chuỗi.Khớp với Regex R hoặc Regex S. ... Khoa học dữ liệu thực tế bằng cách sử dụng Python ..

Làm thế nào để bạn chèn một nhân vật đặc biệt trong Python?

Trong các chuỗi Python, dấu gạch chéo ngược "\" là một nhân vật đặc biệt, còn được gọi là nhân vật "Escape".Nó được sử dụng để thể hiện các ký tự khoảng trắng nhất định: "\ t" là một tab, "\ n" là một dòng mới và "\ r" là một sự trở lại vận chuyển.Ngược lại, tiền tố một ký tự đặc biệt với "\" biến nó thành một ký tự thông thường.the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return. Conversely, prefixing a special character with "\" turns it into an ordinary character.

Làm thế nào để bạn thêm một ký tự đặc biệt vào một chuỗi trong Python?

Để chèn các ký tự là bất hợp pháp trong một chuỗi, hãy sử dụng một ký tự thoát.Một nhân vật thoát là một dấu gạch chéo ngược \ theo sau là nhân vật bạn muốn chèn.use an escape character. An escape character is a backslash \ followed by the character you want to insert.

'$' Có nghĩa là gì ở Regex?

$ có nghĩa là "khớp với phần cuối của chuỗi" (vị trí sau ký tự cuối cùng trong chuỗi).Cả hai đều được gọi là neo và đảm bảo rằng toàn bộ chuỗi được khớp thay vì chỉ là một chuỗi con.Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.