GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Python re.split - Mastering String Splitting with Regular Expressions Python re.split Functionlast modified April 20, 2025 Introduction to re.split
The
Unlike The function returns a list of substrings obtained by splitting the input string at each match of the pattern. Empty matches result in empty strings. Basic Syntax
The syntax for re.split(pattern, string, maxsplit=0, flags=0)
Basic String SplittingLet's start with a simple example splitting on whitespace. basic_split.py
#!/usr/bin/python
import re
text = "apple banana cherry date"
result = re.split(r'\s+', text)
print("Split result:", result)
This splits the string at each sequence of whitespace characters.
The result = re.split(r'\s+', text)
The Splitting on Multiple Delimiters
multi_delimiter.py
#!/usr/bin/python
import re
text = "apple,banana;cherry date:fig"
result = re.split(r'[,;:\s]+', text)
print("Split result:", result)
This splits on commas, semicolons, colons, or whitespace. The character
class Limiting Splits with maxsplit
The maxsplit_example.py
#!/usr/bin/python
import re
text = "one two three four five six"
result = re.split(r'\s+', text, maxsplit=2)
print("Limited split:", result)
With Splitting with Capture GroupsWhen patterns contain capture groups, the groups are included in the result. capture_groups.py
#!/usr/bin/python
import re
text = "appleXbananaYcherryZdate"
result = re.split(r'([XYZ])', text)
print("Split with delimiters:", result)
The parentheses create a capture group. The matched delimiters (X, Y, Z) appear in the result list between the split segments. Splitting on Word Boundaries
Word boundaries ( word_boundaries.py
#!/usr/bin/python
import re
text = "HelloWorldThisIsCamelCase"
result = re.split(r'(?<=[a-z])(?=[A-Z])', text)
print("CamelCase split:", result)
This splits camelCase words by looking for lowercase-to-uppercase transitions. The lookbehind and lookahead assertions don't consume chars. Splitting While Keeping DelimitersUsing lookahead/lookbehind assertions preserves delimiters in the output. keep_delimiters.py
#!/usr/bin/python
import re
text = "20+30-40*50/60"
result = re.split(r'(?<=[+*/-])|(?=[+*/-])', text)
print("Split with operators:", result)
This splits mathematical expressions while keeping operators. The pattern matches positions before or after operators without consuming them. Splitting with FlagsFlags can modify splitting behavior, like case-insensitive matching. flags_example.py
#!/usr/bin/python
import re
text = "appleBananaCHERRYdateFIG"
result = re.split(r'[A-Z]+', text, flags=re.IGNORECASE)
print("Case-insensitive split:", result)
The Best Practices
When using
Performance Considerations
For simple splits with fixed strings,
Compiling patterns with SourcePython re.split() documentation
This tutorial covered the essential aspects of Python's AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Python tutorials. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |