Hướng dẫn python regex global flag - cờ toàn cầu python regex

40

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Có một lá cờ hoặc một số khóa đặc biệt trong Python để sử dụng mẫu nhiều lần. Tôi đã từng kiểm tra http://gskinner.com/regexr/ regexp của tôi, nó hoạt động chính xác trong đó. Nhưng khi kiểm tra trong khớp chính xác chỉ trả về không.

import re
pattern = r"(?P--\d\d-\w+:\d\d)[ \t]+(?P\w+)[ \t]+(?P[\w ]+)[\" ]*    (?P[\w\\:\.]+)@@(?P[\w\\]+)[\" ]*(?P[\(\w, \.\)]+){0,1}[\s \"]*(?P[\w \.-]+){0,1}[\"]"
base = """
--02-21T11:22  user3   create version "W:\foo\bar\fooz.bat@@\main\1" (label1, label2,   label3, label22, label33, ...)

"merge in new bat-based fooz installer"

--02-21T11:22  user1   create version "W:\foo\bar\fooz.bat@@\main\0"

--02-21T11:22  user2   create branch "W:\foo\bar\fooz.bat@@\main\"

"merge in new bat-based fooz installer"

--02-13T11:22  user1   create version     "W:\foo\bar\fooz.bat@@\main\1"

  "Made to use new fooz.bat"

"""
r = re.match(pattern, base)
print(r)

Hướng dẫn python regex global flag - cờ toàn cầu python regex

Tomalak

326K66 Huy hiệu vàng516 Huy hiệu bạc619 Huy hiệu Đồng66 gold badges516 silver badges619 bronze badges

hỏi ngày 27 tháng 7 năm 2012 lúc 11:05Jul 27, 2012 at 11:05

0

re.findall(pattern, string, flags=0)
0 cố gắng khớp với mẫu khi bắt đầu chuỗi. Bạn đang tìm kiếm
re.findall(pattern, string, flags=0)
1,
re.findall(pattern, string, flags=0)
2 hoặc
re.findall(pattern, string, flags=0)
3

Alex Gyoshev

11.8K4 Huy hiệu vàng48 Huy hiệu bạc74 Huy hiệu đồng4 gold badges48 silver badges74 bronze badges

Đã trả lời ngày 27 tháng 7 năm 2012 lúc 11:33Jul 27, 2012 at 11:33

JariymariyMariy

5.4863 Huy hiệu vàng40 Huy hiệu bạc57 Huy hiệu đồng3 gold badges40 silver badges57 bronze badges

0

Mỗi chức năng phù hợp với biểu thức thông thường Python đều hữu ích cho các mục đích khác nhau.

re.findall(pattern, string, flags=0)
0 luôn bắt đầu ở đầu chuỗi.

re.findall(pattern, string, flags=0)
1 bước qua chuỗi từ đầu tìm kiếm trận đấu đầu tiên. Nó dừng lại khi nó tìm thấy một trận đấu.

re.findall(pattern, string, flags=0)
2 Trả về một danh sách tất cả các kết quả tìm kiếm.

Trong tất cả các trường hợp ở trên, nếu có một nhóm trong mẫu regex, thì mục bạn nhận lại là một phần của trận đấu đầy đủ, theo sau mỗi nhóm phù hợp theo thứ tự chúng xuất hiện trong mẫu regex.

Đã trả lời ngày 18 tháng 1 năm 2020 lúc 20:11Jan 18, 2020 at 20:11

Hướng dẫn python regex global flag - cờ toàn cầu python regex

Trong nhiều chức năng Python Regex, bạn thấy một lá cờ đối số thứ ba. Họ là gì và họ làm việc như thế nào?

Cờ Python Regex [Hướng dẫn cuối cùng]

Cờ cho phép bạn điều khiển động cơ biểu thức thông thường. Bởi vì các biểu thức thông thường rất mạnh mẽ, chúng là một cách hữu ích để bật và tắt một số tính năng nhất định (ví dụ: có nên bỏ qua việc viết hoa khi khớp với regex của bạn).

Ví dụ: ở đây, cách thức các cờ đối số thứ ba được sử dụng trong phương thức

re.findall(pattern, string, flags=0)
7:

re.findall(pattern, string, flags=0)

Vì vậy, đối số cờ dường như là một đối số số nguyên với giá trị mặc định là 0. Để kiểm soát hành vi Regex mặc định, bạn chỉ cần sử dụng một trong các giá trị số nguyên được xác định trước. Bạn có thể truy cập các giá trị được xác định trước này thông qua thư viện RE:

Cú pháp Nghĩa
re.findall(pattern, string, flags=0)
8
Nếu bạn không sử dụng lá cờ này, các biểu tượng Python Regex đặc biệt
re.findall(pattern, string, flags=0)
9,
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('HER', text, flags=re.IGNORECASE))
# ['her', 'Her', 'her', 'her']
0,
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('HER', text, flags=re.IGNORECASE))
# ['her', 'Her', 'her', 'her']
1,
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('HER', text, flags=re.IGNORECASE))
# ['her', 'Her', 'her', 'her']
2,
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('HER', text, flags=re.IGNORECASE))
# ['her', 'Her', 'her', 'her']
3,
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('HER', text, flags=re.IGNORECASE))
# ['her', 'Her', 'her', 'her']
4,
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('HER', text, flags=re.IGNORECASE))
# ['her', 'Her', 'her', 'her']
5 và
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('HER', text, flags=re.IGNORECASE))
# ['her', 'Her', 'her', 'her']
6 sẽ khớp với các ký tự Unicode. Nếu bạn sử dụng cờ này, những biểu tượng đặc biệt đó sẽ chỉ khớp với các ký tự ASCII - như tên cho thấy.
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('HER', text, flags=re.IGNORECASE))
# ['her', 'Her', 'her', 'her']
7
Giống như
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('HER', text, flags=re.IGNORECASE))
# ['her', 'Her', 'her', 'her']
8
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('HER', text, flags=re.IGNORECASE))
# ['her', 'Her', 'her', 'her']
9
Nếu bạn sử dụng cờ này, Python sẽ in một số thông tin hữu ích vào vỏ giúp bạn gỡ lỗi Regex của mình.
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('   HER   # Ignored', text,
                 flags=re.IGNORECASE + re.VERBOSE))
# ['her', 'Her', 'her', 'her']
0
Nếu bạn sử dụng lá cờ này, động cơ Regex sẽ thực hiện khớp không nhạy cảm với trường hợp. Vì vậy, nếu bạn đang tìm kiếm lớp nhân vậ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('   HER   # Ignored', text,
                 flags=re.IGNORECASE + re.VERBOSE))
# ['her', 'Her', 'her', 'her']
1, nó cũng sẽ khớp với
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('   HER   # Ignored', text,
                 flags=re.IGNORECASE + re.VERBOSE))
# ['her', 'Her', 'her', 'her']
2.
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('   HER   # Ignored', text,
                 flags=re.IGNORECASE + re.VERBOSE))
# ['her', 'Her', 'her', 'her']
3
Giống như
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('   HER   # Ignored', text,
                 flags=re.IGNORECASE + re.VERBOSE))
# ['her', 'Her', 'her', 'her']
4
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('   HER   # Ignored', text,
                 flags=re.IGNORECASE + re.VERBOSE))
# ['her', 'Her', 'her', 'her']
5
Don lồng sử dụng lá cờ này - bao giờ. Nó đã khấu hao, ý tưởng là thực hiện kết hợp không nhạy cảm với trường hợp tùy thuộc vào địa phương hiện tại của bạn. Nhưng nó không đáng tin cậy.
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('   HER   # Ignored', text,
                 flags=re.IGNORECASE + re.VERBOSE))
# ['her', 'Her', 'her', 'her']
6
Giống như re.locale
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('   HER   # Ignored', text,
                 flags=re.IGNORECASE + re.VERBOSE))
# ['her', 'Her', 'her', 'her']
7
Cờ này chuyển đổi trên tính năng sau: Regex
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('   HER   # Ignored', text,
                 flags=re.IGNORECASE + re.VERBOSE))
# ['her', 'Her', 'her', 'her']
8 bắt đầu khớp ở đầu mỗi dòng (thay vì chỉ ở đầu chuỗi). Tương tự như vậy cho Regex
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('   HER   # Ignored', text,
                 flags=re.IGNORECASE + re.VERBOSE))
# ['her', 'Her', 'her', 'her']
9 cuối chuỗi hiện đang khớp với cuối mỗi dòng trong một chuỗi nhiều dòng.
#########################
# re.ASCII
#########################

s = 'hello wörld'

print(re.findall('\w+', s))
# ['hello', 'wörld']

print(re.findall('\w+', s, flags=re.ASCII))
# ['hello', 'w', 'rld']
0
Giống như
#########################
# re.ASCII
#########################

s = 'hello wörld'

print(re.findall('\w+', s))
# ['hello', 'wörld']

print(re.findall('\w+', s, flags=re.ASCII))
# ['hello', 'w', 'rld']
1
#########################
# re.ASCII
#########################

s = 'hello wörld'

print(re.findall('\w+', s))
# ['hello', 'wörld']

print(re.findall('\w+', s, flags=re.ASCII))
# ['hello', 'w', 'rld']
2
Không cần sử dụng cờ này, dấu chấm Regex
#########################
# re.ASCII
#########################

s = 'hello wörld'

print(re.findall('\w+', s))
# ['hello', 'wörld']

print(re.findall('\w+', s, flags=re.ASCII))
# ['hello', 'w', 'rld']
3 phù hợp với tất cả các ký tự ngoại trừ ký tự mới
#########################
# re.ASCII
#########################

s = 'hello wörld'

print(re.findall('\w+', s))
# ['hello', 'wörld']

print(re.findall('\w+', s, flags=re.ASCII))
# ['hello', 'w', 'rld']
4. Bật cờ này để thực sự khớp với tất cả các ký tự bao gồm cả ký tự mới.
#########################
# re.ASCII
#########################

s = 'hello wörld'

print(re.findall('\w+', s))
# ['hello', 'wörld']

print(re.findall('\w+', s, flags=re.ASCII))
# ['hello', 'w', 'rld']
5
Giống như
#########################
# re.ASCII
#########################

s = 'hello wörld'

print(re.findall('\w+', s))
# ['hello', 'wörld']

print(re.findall('\w+', s, flags=re.ASCII))
# ['hello', 'w', 'rld']
6
#########################
# re.ASCII
#########################

s = 'hello wörld'

print(re.findall('\w+', s))
# ['hello', 'wörld']

print(re.findall('\w+', s, flags=re.ASCII))
# ['hello', 'w', 'rld']
7
Để cải thiện khả năng đọc của các biểu thức chính quy phức tạp, bạn có thể muốn cho phép nhận xét và định dạng (đa dòng) của chính Regex. Điều này là có thể với lá cờ này: tất cả các ký tự và dòng khoảng trắng bắt đầu với ký tự
#########################
# re.ASCII
#########################

s = 'hello wörld'

print(re.findall('\w+', s))
# ['hello', 'wörld']

print(re.findall('\w+', s, flags=re.ASCII))
# ['hello', 'w', 'rld']
8 đều bị bỏ qua trong Regex.
#########################
# re.ASCII
#########################

s = 'hello wörld'

print(re.findall('\w+', s))
# ['hello', 'wörld']

print(re.findall('\w+', s, flags=re.ASCII))
# ['hello', 'w', 'rld']
9
Giống như
#########################
# re.DEBUG
#########################

s = 'hello world'

print(re.findall('\w+', s, flags=re.DEBUG))
'''
MAX_REPEAT 1 MAXREPEAT
  IN
    CATEGORY CATEGORY_WORD

 0. INFO 4 0b0 1 MAXREPEAT (to 5)
 5: REPEAT_ONE 9 1 MAXREPEAT (to 15)
 9.   IN 4 (to 14)
11.     CATEGORY UNI_WORD
13.     FAILURE
14:   SUCCESS
15: SUCCESS
['hello', 'world']
'''
0

  • Làm thế nào để sử dụng những lá cờ này?
  • Làm thế nào để sử dụng nhiều cờ?
  • re.ASCII
  • re.DEBUG
  • re.IGNORECASE
  • re.MULTILINE
  • re.DOTALL
  • re.VERBOSE

Làm thế nào để sử dụng những lá cờ này?

Làm thế nào để sử dụng nhiều cờ?

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('HER', text, flags=re.IGNORECASE))
# ['her', 'Her', 'her', 'her']

Chỉ cần bao gồm cờ làm đối số

#########################
# re.DEBUG
#########################

s = 'hello world'

print(re.findall('\w+', s, flags=re.DEBUG))
'''
MAX_REPEAT 1 MAXREPEAT
  IN
    CATEGORY CATEGORY_WORD

 0. INFO 4 0b0 1 MAXREPEAT (to 5)
 5: REPEAT_ONE 9 1 MAXREPEAT (to 15)
 9.   IN 4 (to 14)
11.     CATEGORY UNI_WORD
13.     FAILURE
14:   SUCCESS
15: SUCCESS
['hello', 'world']
'''
1 tùy chọn như sau:

Như bạn thấy, cờ Re.ignorecase đảm bảo rằng tất cả các lần xuất hiện của chuỗi ’của cô ấy được khớp với nhau, không quan trọng về vốn hóa của họ. Python Regex Superpower – The Ultimate Guide

Bài viết liên quan: Siêu cường Python Regex - Hướng dẫn cuối cùng 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.

Làm thế nào để sử dụng nhiều cờ?

Chỉ cần bao gồm cờ làm đối số

#########################
# re.DEBUG
#########################

s = 'hello world'

print(re.findall('\w+', s, flags=re.DEBUG))
'''
MAX_REPEAT 1 MAXREPEAT
  IN
    CATEGORY CATEGORY_WORD

 0. INFO 4 0b0 1 MAXREPEAT (to 5)
 5: REPEAT_ONE 9 1 MAXREPEAT (to 15)
 9.   IN 4 (to 14)
11.     CATEGORY UNI_WORD
13.     FAILURE
14:   SUCCESS
15: SUCCESS
['hello', 'world']
'''
1 tùy chọn như sau:

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('   HER   # Ignored', text,
                 flags=re.IGNORECASE + re.VERBOSE))
# ['her', 'Her', 'her', 'her']

Như bạn thấy, cờ Re.ignorecase đảm bảo rằng tất cả các lần xuất hiện của chuỗi ’của cô ấy được khớp với nhau, không quan trọng về vốn hóa của họ.

Bài viết liên quan: Siêu cường Python Regex - Hướng dẫn cuối cùng

re.ASCII

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 .

#########################
# re.ASCII
#########################

s = 'hello wörld'

print(re.findall('\w+', s))
# ['hello', 'wörld']

print(re.findall('\w+', s, flags=re.ASCII))
# ['hello', 'w', 'rld']

re.DEBUG

Có, chỉ cần thêm chúng lại với nhau (tổng hợp chúng) như sau:

#########################
# re.DEBUG
#########################

s = 'hello world'

print(re.findall('\w+', s, flags=re.DEBUG))
'''
MAX_REPEAT 1 MAXREPEAT
  IN
    CATEGORY CATEGORY_WORD

 0. INFO 4 0b0 1 MAXREPEAT (to 5)
 5: REPEAT_ONE 9 1 MAXREPEAT (to 15)
 9.   IN 4 (to 14)
11.     CATEGORY UNI_WORD
13.     FAILURE
14:   SUCCESS
15: SUCCESS
['hello', 'world']
'''

re.IGNORECASE

Bạn sử dụng cả hai lá cờ

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('   HER   # Ignored', text,
                 flags=re.IGNORECASE + re.VERBOSE))
# ['her', 'Her', 'her', 'her']
4 (tất cả các sự xuất hiện của các biến thể chuỗi dưới hoặc chữ hoa của ’HER của cô ấy được khớp) và
#########################
# re.DEBUG
#########################

s = 'hello world'

print(re.findall('\w+', s, flags=re.DEBUG))
'''
MAX_REPEAT 1 MAXREPEAT
  IN
    CATEGORY CATEGORY_WORD

 0. INFO 4 0b0 1 MAXREPEAT (to 5)
 5: REPEAT_ONE 9 1 MAXREPEAT (to 15)
 9.   IN 4 (to 14)
11.     CATEGORY UNI_WORD
13.     FAILURE
14:   SUCCESS
15: SUCCESS
['hello', 'world']
'''
3 (bỏ qua các bình luận và khoảng trắng trong Regex). Bạn tổng hợp chúng với nhau
#########################
# re.DEBUG
#########################

s = 'hello world'

print(re.findall('\w+', s, flags=re.DEBUG))
'''
MAX_REPEAT 1 MAXREPEAT
  IN
    CATEGORY CATEGORY_WORD

 0. INFO 4 0b0 1 MAXREPEAT (to 5)
 5: REPEAT_ONE 9 1 MAXREPEAT (to 15)
 9.   IN 4 (to 14)
11.     CATEGORY UNI_WORD
13.     FAILURE
14:   SUCCESS
15: SUCCESS
['hello', 'world']
'''
4 để chỉ ra rằng bạn muốn lấy cả hai.

#########################
# re.IGNORECASE
##########################

s = 'HELLO world'

print(re.findall('[a-z]+', s))
# ['world']

print(re.findall('[a-z]+', s, flags=re.IGNORECASE))
# ['HELLO', 'world']

re.MULTILINE

Ví dụ, hãy đi sâu vào các lá cờ khác nhau.

#########################
# re.MULTILINE
#########################

s = '''hello
world'''

print(re.findall('^[a-z]+', s))
# ['hello']

print(re.findall('^[a-z]+', s, flags=re.MULTILINE))
# ['hello', 'world']

re.DOTALL

Nếu bạn không sử dụng cờ này, các biểu tượng Python Regex đặc biệt \ w, \ w, \ b, \ b, \ d, \ d, \ s và \ s sẽ khớp với các ký tự Unicode. Nếu bạn sử dụng cờ này, những biểu tượng đặc biệt đó sẽ chỉ khớp với các ký tự ASCII - như tên cho thấy.

#########################
# re.DOTALL
#########################

s = '''hello
world'''

print(re.findall('.+', s))
# ['hello', 'world']

print(re.findall('.*', s, flags=re.DOTALL))
# ['hello\nworld', '']

re.VERBOSE

Để cải thiện khả năng đọc của các biểu thức chính quy phức tạp, bạn có thể muốn cho phép nhận xét và định dạng (đa dòng) của chính Regex. Điều này là có thể với lá cờ này: tất cả các ký tự và dòng khoảng trắng bắt đầu với ký tự ‘#, bị bỏ qua trong regex.

#########################
# re.VERBOSE
#########################

s = 'hello world'

print(re.findall('.+ #I can now write comments', s, flags=re.VERBOSE))
# ['hello world']

Các kỹ sư của Google, Facebook và Amazon là những bậc thầy biểu hiện thường xuyên. Nếu bạn cũng muốn trở thành một, hãy xem cuốn sách mới của chúng tôi: Cách thông minh nhất để tìm hiểu Python Regex (Amazon Kindle/Print, Mở trong Tab mới).The Smartest Way to Learn Python Regex (Amazon Kindle/Print, opens in new tab).

Hướng dẫn python regex global flag - cờ toàn cầu python regex

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.