Hướng dẫn regex split words python - regex chia từ python

Trong bài viết này, sẽ học cách phân chia một chuỗi dựa trên một mẫu biểu thức thông thường trong Python. Phương pháp Pythons Re Module ____ ____10 chia tách chuỗi bằng các lần xuất hiện của mẫu regex, trả về một danh sách chứa các chuỗi con kết quả.split the string by the occurrences of the regex pattern, returning a list containing the resulting substrings.

Sau khi đọc bài viết này, bạn sẽ có thể thực hiện các hoạt động phân chia sau bằng Regex trong Python.

Hoạt độngSự mô tả
import re

target_string = "My name is maximums and my luck numbers are 12 45 78"
# split on white-space 
word_list = re.split(r"\s+", target_string)
print(word_list)

# Output ['My', 'name', 'is', 'maximums', 'and', 'my', 'luck', 'numbers', 'are', '12', '45', '78']
1
Chia chuỗi theo mỗi lần xuất hiện của
import re

target_string = "My name is maximums and my luck numbers are 12 45 78"
# split on white-space 
word_list = re.split(r"\s+", target_string)
print(word_list)

# Output ['My', 'name', 'is', 'maximums', 'and', 'my', 'luck', 'numbers', 'are', '12', '45', '78']
2.
import re

target_string = "My name is maximums and my luck numbers are 12 45 78"
# split on white-space 
word_list = re.split(r"\s+", target_string)
print(word_list)

# Output ['My', 'name', 'is', 'maximums', 'and', 'my', 'luck', 'numbers', 'are', '12', '45', '78']
3
Chia chuỗi theo các lần xuất hiện của
import re

target_string = "My name is maximums and my luck numbers are 12 45 78"
# split on white-space 
word_list = re.split(r"\s+", target_string)
print(word_list)

# Output ['My', 'name', 'is', 'maximums', 'and', 'my', 'luck', 'numbers', 'are', '12', '45', '78']
2. Giới hạn số lượng phân tách ở 2
import re

target_string = "My name is maximums and my luck numbers are 12 45 78"
# split on white-space 
word_list = re.split(r"\s+", target_string)
print(word_list)

# Output ['My', 'name', 'is', 'maximums', 'and', 'my', 'luck', 'numbers', 'are', '12', '45', '78']
5
Chia chuỗi phân chia theo nhiều mẫu phân cách (________ 16 & nbsp; và
import re

target_string = "My name is maximums and my luck numbers are 12 45 78"
# split on white-space 
word_list = re.split(r"\s+", target_string)
print(word_list)

# Output ['My', 'name', 'is', 'maximums', 'and', 'my', 'luck', 'numbers', 'are', '12', '45', '78']
7).
Python Regex Split Operations

Cách sử dụng hàm import re target_string = "My name is maximums and my luck numbers are 12 45 78" # split on white-space word_list = re.split(r"\s+", target_string) print(word_list) # Output ['My', 'name', 'is', 'maximums', 'and', 'my', 'luck', 'numbers', 'are', '12', '45', '78']0

Trước khi di chuyển xa hơn, hãy để Lừa xem cú pháp của Phương thức Python, ____ 10 & nbsp;

Cú pháp

re.split(pattern, string, maxsplit=0, flags=0)

Mẫu biểu thức chính quy và chuỗi đích là các đối số bắt buộc.

import re

target_string = "12-45-78"

# Split only on the first occurrence
# maxsplit is 1
result = re.split(r"\D", target_string, maxsplit=1)
print(result)
# Output ['12', '45-78']

# Split on the three occurrence
# maxsplit is 3
result = re.split(r"\D", target_string, maxsplit=3)
print(result)
# Output ['12', '45', '78']
0 và cờ là tùy chọn.

  • import re
    
    target_string = "My name is maximums and my luck numbers are 12 45 78"
    # split on white-space 
    word_list = re.split(r"\s+", target_string)
    print(word_list)
    
    # Output ['My', 'name', 'is', 'maximums', 'and', 'my', 'luck', 'numbers', 'are', '12', '45', '78']
    2: mẫu biểu thức chính quy được sử dụng để phân tách chuỗi đích.
  • import re
    
    target_string = "12-45-78"
    
    # Split only on the first occurrence
    # maxsplit is 1
    result = re.split(r"\D", target_string, maxsplit=1)
    print(result)
    # Output ['12', '45-78']
    
    # Split on the three occurrence
    # maxsplit is 3
    result = re.split(r"\D", target_string, maxsplit=3)
    print(result)
    # Output ['12', '45', '78']
    2: Biến chỉ vào chuỗi đích (nghĩa là, chuỗi chúng tôi muốn chia).
  • import re
    
    target_string = "12-45-78"
    
    # Split only on the first occurrence
    # maxsplit is 1
    result = re.split(r"\D", target_string, maxsplit=1)
    print(result)
    # Output ['12', '45-78']
    
    # Split on the three occurrence
    # maxsplit is 3
    result = re.split(r"\D", target_string, maxsplit=3)
    print(result)
    # Output ['12', '45', '78']
    0: Số lượng phân chia bạn muốn thực hiện. Nếu
    import re
    
    target_string = "12-45-78"
    
    # Split only on the first occurrence
    # maxsplit is 1
    result = re.split(r"\D", target_string, maxsplit=1)
    print(result)
    # Output ['12', '45-78']
    
    # Split on the three occurrence
    # maxsplit is 3
    result = re.split(r"\D", target_string, maxsplit=3)
    print(result)
    # Output ['12', '45', '78']
    0 là 2, nhiều nhất là hai lần chia tách và phần còn lại của chuỗi được trả về làm yếu tố cuối cùng của danh sách.
  • import re
    
    target_string = "12-45-78"
    
    # Split only on the first occurrence
    # maxsplit is 1
    result = re.split(r"\D", target_string, maxsplit=1)
    print(result)
    # Output ['12', '45-78']
    
    # Split on the three occurrence
    # maxsplit is 3
    result = re.split(r"\D", target_string, maxsplit=3)
    print(result)
    # Output ['12', '45', '78']
    5: Theo mặc định, không có cờ nào được áp dụng. Có nhiều cờ Regex chúng ta có thể sử dụng. Ví dụ,
    import re
    
    target_string = "12-45-78"
    
    # Split only on the first occurrence
    # maxsplit is 1
    result = re.split(r"\D", target_string, maxsplit=1)
    print(result)
    # Output ['12', '45-78']
    
    # Split on the three occurrence
    # maxsplit is 3
    result = re.split(r"\D", target_string, maxsplit=3)
    print(result)
    # Output ['12', '45', '78']
    6 được sử dụng để thực hiện tìm kiếm không nhạy cảm trường hợp.
    There are many regex flags we can use. For example, the
    import re
    
    target_string = "12-45-78"
    
    # Split only on the first occurrence
    # maxsplit is 1
    result = re.split(r"\D", target_string, maxsplit=1)
    print(result)
    # Output ['12', '45-78']
    
    # Split on the three occurrence
    # maxsplit is 3
    result = re.split(r"\D", target_string, maxsplit=3)
    print(result)
    # Output ['12', '45', '78']
    6 is used for performing case-insensitive searching.

Lưu ý: Nếu chụp dấu ngoặc đơn được sử dụng trong mẫu, thì văn bản của tất cả các nhóm trong mẫu cũng được trả về như một phần của danh sách kết quả.: If capturing parentheses are used in the pattern, then the text of all groups in the pattern is also returned as part of the resulting list.

Giá trị trả về

Nó chia chuỗi mục tiêu theo mẫu biểu thức chính quy và các trận đấu được trả về dưới dạng danh sách.list.

Nếu mẫu được chỉ định không được tìm thấy bên trong chuỗi đích, thì chuỗi không được phân chia theo bất kỳ cách nào, nhưng phương thức chia vẫn tạo ra một danh sách vì đây là cách mà nó thiết kế. Tuy nhiên, danh sách chỉ chứa một phần tử, chính chuỗi đích.

Ví dụ regex để chia chuỗi thành từ

Bây giờ, hãy để Lừa xem cách sử dụng

import re

target_string = "My name is maximums and my luck numbers are 12 45 78"
# split on white-space 
word_list = re.split(r"\s+", target_string)
print(word_list)

# Output ['My', 'name', 'is', 'maximums', 'and', 'my', 'luck', 'numbers', 'are', '12', '45', '78']
0 với sự trợ giúp của một ví dụ đơn giản. Trong ví dụ này, chúng tôi sẽ phân chia chuỗi mục tiêu ở mỗi ký tự không gian trắng bằng cách sử dụng chuỗi đặc biệt
import re

target_string = "12-45-78"

# Split only on the first occurrence
# maxsplit is 1
result = re.split(r"\D", target_string, maxsplit=1)
print(result)
# Output ['12', '45-78']

# Split on the three occurrence
# maxsplit is 3
result = re.split(r"\D", target_string, maxsplit=3)
print(result)
# Output ['12', '45', '78']
8. white-space character using the
import re

target_string = "12-45-78"

# Split only on the first occurrence
# maxsplit is 1
result = re.split(r"\D", target_string, maxsplit=1)
print(result)
# Output ['12', '45-78']

# Split on the three occurrence
# maxsplit is 3
result = re.split(r"\D", target_string, maxsplit=3)
print(result)
# Output ['12', '45', '78']
8 special sequence.

Hãy để thêm Metacharacter

import re

target_string = "12-45-78"

# Split only on the first occurrence
# maxsplit is 1
result = re.split(r"\D", target_string, maxsplit=1)
print(result)
# Output ['12', '45-78']

# Split on the three occurrence
# maxsplit is 3
result = re.split(r"\D", target_string, maxsplit=3)
print(result)
# Output ['12', '45', '78']
9 vào cuối
import re

target_string = "12-45-78"

# Split only on the first occurrence
# maxsplit is 1
result = re.split(r"\D", target_string, maxsplit=1)
print(result)
# Output ['12', '45-78']

# Split on the three occurrence
# maxsplit is 3
result = re.split(r"\D", target_string, maxsplit=3)
print(result)
# Output ['12', '45', '78']
8. Bây giờ, mẫu Regex
import re

target_string = "12,45,78,85-17-89"
# 2 delimiter - and ,
# use OR (|) operator to combine two pattern
result = re.split(r"-|,", target_string)
print(result)
# Output ['12', '45', '78', '85', '17', '89']
1 sẽ phân chia chuỗi mục tiêu về sự xuất hiện của một hoặc nhiều ký tự khoảng trắng. Hãy cùng xem bản demo.

Thí dụ

import re

target_string = "My name is maximums and my luck numbers are 12 45 78"
# split on white-space 
word_list = re.split(r"\s+", target_string)
print(word_list)

# Output ['My', 'name', 'is', 'maximums', 'and', 'my', 'luck', 'numbers', 'are', '12', '45', '78']

Như bạn có thể thấy trong đầu ra, chúng tôi có danh sách các từ được phân tách bằng khoảng trắng.

Giới hạn số lượng chia tách

Tham số

import re

target_string = "12-45-78"

# Split only on the first occurrence
# maxsplit is 1
result = re.split(r"\D", target_string, maxsplit=1)
print(result)
# Output ['12', '45-78']

# Split on the three occurrence
# maxsplit is 3
result = re.split(r"\D", target_string, maxsplit=3)
print(result)
# Output ['12', '45', '78']
0 của
import re

target_string = "My name is maximums and my luck numbers are 12 45 78"
# split on white-space 
word_list = re.split(r"\s+", target_string)
print(word_list)

# Output ['My', 'name', 'is', 'maximums', 'and', 'my', 'luck', 'numbers', 'are', '12', '45', '78']
0 được sử dụng để xác định số lượng phân chia bạn muốn thực hiện.

Nói một cách đơn giản, nếu

import re

target_string = "12-45-78"

# Split only on the first occurrence
# maxsplit is 1
result = re.split(r"\D", target_string, maxsplit=1)
print(result)
# Output ['12', '45-78']

# Split on the three occurrence
# maxsplit is 3
result = re.split(r"\D", target_string, maxsplit=3)
print(result)
# Output ['12', '45', '78']
0 là 2, thì hai lần phân tách sẽ được thực hiện và phần còn lại của chuỗi được trả về làm yếu tố cuối cùng của danh sách.if the
import re

target_string = "12-45-78"

# Split only on the first occurrence
# maxsplit is 1
result = re.split(r"\D", target_string, maxsplit=1)
print(result)
# Output ['12', '45-78']

# Split on the three occurrence
# maxsplit is 3
result = re.split(r"\D", target_string, maxsplit=3)
print(result)
# Output ['12', '45', '78']
0 is 2, then two splits will be done
, and the remainder of the string is returned as the final element of the list.

Vì vậy, hãy để một ví dụ đơn giản để phân chia một chuỗi về sự xuất hiện của bất kỳ chữ số nào. Ở đây chúng tôi sẽ sử dụng trình tự đặc biệt

import re

target_string = "12,45,78,85-17-89"
# 2 delimiter - and ,
# use OR (|) operator to combine two pattern
result = re.split(r"-|,", target_string)
print(result)
# Output ['12', '45', '78', '85', '17', '89']
5 phù hợp với bất kỳ ký tự không chữ số nào.

Thí dụ

import re

target_string = "12-45-78"

# Split only on the first occurrence
# maxsplit is 1
result = re.split(r"\D", target_string, maxsplit=1)
print(result)
# Output ['12', '45-78']

# Split on the three occurrence
# maxsplit is 3
result = re.split(r"\D", target_string, maxsplit=3)
print(result)
# Output ['12', '45', '78']

Như bạn có thể thấy trong đầu ra, chúng tôi có danh sách các từ được phân tách bằng khoảng trắng.

Giới hạn số lượng chia tách

Tham số

import re

target_string = "12-45-78"

# Split only on the first occurrence
# maxsplit is 1
result = re.split(r"\D", target_string, maxsplit=1)
print(result)
# Output ['12', '45-78']

# Split on the three occurrence
# maxsplit is 3
result = re.split(r"\D", target_string, maxsplit=3)
print(result)
# Output ['12', '45', '78']
0 của
import re

target_string = "My name is maximums and my luck numbers are 12 45 78"
# split on white-space 
word_list = re.split(r"\s+", target_string)
print(word_list)

# Output ['My', 'name', 'is', 'maximums', 'and', 'my', 'luck', 'numbers', 'are', '12', '45', '78']
0 được sử dụng để xác định số lượng phân chia bạn muốn thực hiện.

Nói một cách đơn giản, nếu

import re

target_string = "12-45-78"

# Split only on the first occurrence
# maxsplit is 1
result = re.split(r"\D", target_string, maxsplit=1)
print(result)
# Output ['12', '45-78']

# Split on the three occurrence
# maxsplit is 3
result = re.split(r"\D", target_string, maxsplit=3)
print(result)
# Output ['12', '45', '78']
0 là 2, thì hai lần phân tách sẽ được thực hiện và phần còn lại của chuỗi được trả về làm yếu tố cuối cùng của danh sách.

Vì vậy, hãy để một ví dụ đơn giản để phân chia một chuỗi về sự xuất hiện của bất kỳ chữ số nào. Ở đây chúng tôi sẽ sử dụng trình tự đặc biệt

import re

target_string = "12,45,78,85-17-89"
# 2 delimiter - and ,
# use OR (|) operator to combine two pattern
result = re.split(r"-|,", target_string)
print(result)
# Output ['12', '45', '78', '85', '17', '89']
5 phù hợp với bất kỳ ký tự không chữ số nào.

Regex để phân chia chuỗi với nhiều dấu phân cách

import re

target_string = "12,45,78,85-17-89"
# 2 delimiter - and ,
# use OR (|) operator to combine two pattern
result = re.split(r"-|,", target_string)
print(result)
# Output ['12', '45', '78', '85', '17', '89']

Trong phần này, chúng tôi sẽ học cách sử dụng Regex để phân chia một chuỗi trên nhiều trình phân cách trong Python.

Ví dụ: sử dụng phương thức biểu thức thông thường

import re

target_string = "My name is maximums and my luck numbers are 12 45 78"
# split on white-space 
word_list = re.split(r"\s+", target_string)
print(word_list)

# Output ['My', 'name', 'is', 'maximums', 'and', 'my', 'luck', 'numbers', 'are', '12', '45', '78']
0, chúng ta có thể chia chuỗi bằng dấu phẩy hoặc không gian.

import re

target_string = "PYnative   dot.com; is for, Python-developer"
# Pattern to split: [-;,.\s]\s*
result = re.split(r"[-;,.\s]\s*", target_string)
print(result)
# Output ['PYnative', 'dot', 'com', 'is', 'for', 'Python', 'developer']

Với phương pháp Regex

import re

target_string = "12,45,78,85-17-89"
# 2 delimiter - and ,
# use OR (|) operator to combine two pattern
result = re.split(r"-|,", target_string)
print(result)
# Output ['12', '45', '78', '85', '17', '89']
7, bạn sẽ linh hoạt hơn. Bạn có thể chỉ định một mẫu cho các trình phân cách nơi bạn có thể chỉ định nhiều trình phân cách, trong khi với phương thức chuỗi ____ ____37, bạn chỉ có thể sử dụng một ký tự hoặc bộ ký tự cố định để phân chia chuỗi.: we used
import re

target_string = "12,45,78,85-17-89"
# 2 delimiter - and ,
# use OR (|) operator to combine two pattern
result = re.split(r"-|,", target_string)
print(result)
# Output ['12', '45', '78', '85', '17', '89']
9 meta character to indicate a list of delimiter characters. The
import re

target_string = "12,45,78,85-17-89"
# 2 delimiter - and ,
# use OR (|) operator to combine two pattern
result = re.split(r"-|,", target_string)
print(result)
# Output ['12', '45', '78', '85', '17', '89']
9 matches any single character in brackets. For example,
import re

target_string = "PYnative   dot.com; is for, Python-developer"
# Pattern to split: [-;,.\s]\s*
result = re.split(r"[-;,.\s]\s*", target_string)
print(result)
# Output ['PYnative', 'dot', 'com', 'is', 'for', 'Python', 'developer']
1 will match either hyphen, comma, semicolon, dot, and a space character.

Hãy để một ví dụ đơn giản để phân chia chuỗi bằng dấu gạch nối hoặc bằng dấu phẩy.

Ví dụ để phân chia chuỗi bởi hai dấu phân cách

Regex để phân chia chuỗi trên năm dấu phân cách

Thí dụ

import re

target_string = "PYnative! dot.com; is for, Python-developer?"
result = re.split(r"[\b\W\b]+", target_string)
print(result)
# Output ['PYnative', 'dot', 'com', 'is', 'for', 'Python', 'developer', '']

Chia chuỗi của các dấu phân cách và từ cụ thể

import re

text = "12, and45,78and85-17and89-97"
# split by word 'and' space, and comma
result = re.split(r"and|[\s,-]+", text)
print(result)
# Output ['12', '', '45', '78', '85', '17', '89', '97']

Regex chia một chuỗi và giữ các dấu phân cách

Như tôi đã nói với bạn khi bắt đầu bài viết nếu chụp dấu ngoặc đơn được sử dụng trong mẫu, thì văn bản của tất cả các nhóm trong mẫu cũng được trả lại như một phần của danh sách kết quả.

Lưu ý: Bạn đang chụp nhóm bằng cách viết mẫu bên trong ____ 44, ________ 45.: You are capturing the group by writing pattern inside the

import re

target_string = "PYnative   dot.com; is for, Python-developer"
# Pattern to split: [-;,.\s]\s*
result = re.split(r"[-;,.\s]\s*", target_string)
print(result)
# Output ['PYnative', 'dot', 'com', 'is', 'for', 'Python', 'developer']
4,
import re

target_string = "PYnative   dot.com; is for, Python-developer"
# Pattern to split: [-;,.\s]\s*
result = re.split(r"[-;,.\s]\s*", target_string)
print(result)
# Output ['PYnative', 'dot', 'com', 'is', 'for', 'Python', 'developer']
5.

Nói một cách đơn giản, hãy cẩn thận trong khi sử dụng phương pháp

import re

target_string = "My name is maximums and my luck numbers are 12 45 78"
# split on white-space 
word_list = re.split(r"\s+", target_string)
print(word_list)

# Output ['My', 'name', 'is', 'maximums', 'and', 'my', 'luck', 'numbers', 'are', '12', '45', '78']
0 khi mẫu biểu thức chính quy được đặt trong ngoặc đơn để chụp các nhóm. & NBSP; Nếu sử dụng các nhóm chụp, thì văn bản phù hợp cũng được đưa vào danh sách kết quả.

Nó rất hữu ích khi bạn muốn giữ các dấu phân cách/dấu phân cách trong danh sách kết quả.

import re

target_string = "12-45-78."

# Split on non-digit
result = re.split(r"\D+", target_string)
print(result)
# Output ['12', '45', '78', '']

# Split on non-digit and keep the separators
# pattern written in parenthese
result = re.split(r"(\D+)", target_string)
print(result)
# Output ['12', '-', '45', '-', '78', '.', '']

Chuỗi phân tách regex bằng cách bỏ qua trường hợp

Có khả năng chuỗi chứa chữ thường và chữ cái trên.

Ví dụ: bạn muốn chia một chuỗi trên các ký tự hoặc phạm vi ký tự cụ thể, nhưng bạn không biết liệu ký tự/từ đó là chữ hoa hay chữ thường hay kết hợp cả hai. Tại đây, bạn có thể sử dụng cờ

import re

target_string = "PYnative   dot.com; is for, Python-developer"
# Pattern to split: [-;,.\s]\s*
result = re.split(r"[-;,.\s]\s*", target_string)
print(result)
# Output ['PYnative', 'dot', 'com', 'is', 'for', 'Python', 'developer']
7 hoặc
import re

target_string = "12-45-78"

# Split only on the first occurrence
# maxsplit is 1
result = re.split(r"\D", target_string, maxsplit=1)
print(result)
# Output ['12', '45-78']

# Split on the three occurrence
# maxsplit is 3
result = re.split(r"\D", target_string, maxsplit=3)
print(result)
# Output ['12', '45', '78']
6 bên trong phương thức
import re

target_string = "My name is maximums and my luck numbers are 12 45 78"
# split on white-space 
word_list = re.split(r"\s+", target_string)
print(word_list)

# Output ['My', 'name', 'is', 'maximums', 'and', 'my', 'luck', 'numbers', 'are', '12', '45', '78']
0 để thực hiện phân tách không nhạy cảm trường hợp.

import re

# Without ignoring case
print(re.split('[a-z]+', "7J8e7Ss3a"))
# output ['7J8', '7S', '3', '']

# With ignoring case
print(re.split('[a-z]+', "7J8e7Ss3a", flags=re.IGNORECASE))
# output ['7', '8', '7', '3', '']

# Without ignoring case
print(re.split(r"emma", "Emma knows Python.EMMA loves Data Science"))
# output ['Emma knows Python.EMMA loves Data Science']

# With ignoring case
print(re.split(r"emma", "Emma knows Python.EMMA loves Data Science", flags=re.IGNORECASE))
# output ['', ' knows Python.', ' loves Data Science']

Chuỗi phân chia () Phương thức so với Regex Split ()

Bây giờ, hãy để Lừa nghĩ về phương thức

import re

target_string = "12,45,78,85-17-89"
# 2 delimiter - and ,
# use OR (|) operator to combine two pattern
result = re.split(r"-|,", target_string)
print(result)
# Output ['12', '45', '78', '85', '17', '89']
7 mặc định trong Python, đặc trưng cho các chuỗi. Như bạn có thể biết nhiều nhất, phương thức
import re

target_string = "12,45,78,85-17-89"
# 2 delimiter - and ,
# use OR (|) operator to combine two pattern
result = re.split(r"-|,", target_string)
print(result)
# Output ['12', '45', '78', '85', '17', '89']
7 mặc định chia một chuỗi theo một dấu phân cách cụ thể. Tuy nhiên, xin lưu ý rằng dấu phân cách này là một chuỗi cố định mà bạn xác định bên trong dấu ngoặc đơn của phương thức.

Sự khác biệt giữa các phương thức mặc định

import re

target_string = "12,45,78,85-17-89"
# 2 delimiter - and ,
# use OR (|) operator to combine two pattern
result = re.split(r"-|,", target_string)
print(result)
# Output ['12', '45', '78', '85', '17', '89']
7 và các biểu thức thông thường
import re

target_string = "12,45,78,85-17-89"
# 2 delimiter - and ,
# use OR (|) operator to combine two pattern
result = re.split(r"-|,", target_string)
print(result)
# Output ['12', '45', '78', '85', '17', '89']
7 là rất lớn. Có một cách linh hoạt hơn khi sử dụng các biểu thức chính quy được phân chia, có thể chứng minh rất hữu ích trong một số kịch bản và cho các nhiệm vụ cụ thể.

  1. Với phương thức
    import re
    
    target_string = "My name is maximums and my luck numbers are 12 45 78"
    # split on white-space 
    word_list = re.split(r"\s+", target_string)
    print(word_list)
    
    # Output ['My', 'name', 'is', 'maximums', 'and', 'my', 'luck', 'numbers', 'are', '12', '45', '78']
    0, bạn có thể chỉ định một mẫu cho dấu phân cách, trong khi với phương thức mặc định
    import re
    
    target_string = "12,45,78,85-17-89"
    # 2 delimiter - and ,
    # use OR (|) operator to combine two pattern
    result = re.split(r"-|,", target_string)
    print(result)
    # Output ['12', '45', '78', '85', '17', '89']
    7, bạn chỉ có thể sử dụng một ký tự hoặc bộ ký tự cố định.
  2. Ngoài ra, sử dụng
    import re
    
    target_string = "My name is maximums and my luck numbers are 12 45 78"
    # split on white-space 
    word_list = re.split(r"\s+", target_string)
    print(word_list)
    
    # Output ['My', 'name', 'is', 'maximums', 'and', 'my', 'luck', 'numbers', 'are', '12', '45', '78']
    0, chúng ta có thể chia một chuỗi bằng nhiều dấu phân cách.

Chia chuỗi phân chia theo các từ trường hợp trên

Ví dụ: bạn có một chuỗi như là Emma Emma yêu Python và ML, và bạn muốn chia nó bằng các từ chữ hoa để có được kết quả như [‘Xin chào,’ ’thế nào,’ bạn, bạn]

import re

print(re.split(r"\s(?=[A-Z])", "EMMA loves PYTHON and ML"))
# output ['EMMA loves', 'PYTHON and', 'ML']

Giải trình

  • Chúng tôi đã sử dụng lookahead regex
    import re
    
    target_string = "PYnative! dot.com; is for, Python-developer?"
    result = re.split(r"[\b\W\b]+", target_string)
    print(result)
    # Output ['PYnative', 'dot', 'com', 'is', 'for', 'Python', 'developer', '']
    7.
  • Regex này sẽ phân chia ở mọi không gian (____ 28), theo sau là một chuỗi các chữ cái trên ([____ 59]) kết thúc bằng một giới hạn từ (________ 60).