- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do we use a delimiter to split string in Python regular expression?
The re.split() method
re.split(pattern, string, [maxsplit=0]):
This methods helps to split string by the occurrences of given pattern.
import re result=re.split(r'a','Dynamics') print result
Above, we have split the string “Dynamics” by “a”. Method split() has another argument “maxsplit“. It has default value of zero. In this case it does the maximum splits that can be done, but if we give value to maxsplit, it will split the string.
Let’s look at the example below −
import result=re.split(r'a','Dynamics Kinematics') print result
['Dyn', 'mics Kinem', 'tics']
Consider the following code
import re result=re.split(r'i','Dynamics Kinematics',maxsplit=1) print result
['Dyn', 'mics Kinematics']
Here, you can notice that we have fixed the maxsplit to 1. And the result is, it has only two values whereas first example has three values.
- How do we use Python regular expression to match a date string?
- How do we use Python Regular Expression named groups?
- How do we use re.finditer() method in Python regular expression?
- How to split string by a delimiter string in Python?
- Why do we use re.compile() method in Python regular expression?
- How to Split String in Java using Regular Expression?
- Why do we use question mark literal in Python regular expression?
- Java Program to split a string using Regular Expression
- How to split a string with a string delimiter in C#?
- How can we split a string by sentence as a delimiter in Java?
- Why we should use whole string in Java regular expression
- How to use wildcard in Python regular expression?
- How to use range in Python regular expression?
- How to use variables in Python regular expression?
- How to write a Python regular expression to use re.findall()?