While coding or improvising your programming skills, you’ve probably come across many scenarios where you would like to use .split()
in Python so as not to split just one character , but several characters at the same time. Consider this for an example:
"GeeksforGeeks, is an-awesome! Website"
Using .split()
for the above above will result in
[’GeeksforGeeks,’, ’is’,’ an-awesome! ’,’ website’]
whereas the desired result should be
[’GeeksforGeeks’,’ is’, ’an’,’ awesome’, ’website’]
In this article, we’ll look at some of the ways we can achieve the same.
Using re.split ()
This is the most efficient and commonly used method of splitting multiple characters at the same time. A regular expression (regular expressions) is used for this.
|
Output:
The original string is: GeeksforGeeks, is_an-awesome! website
The list after performing split functionality: [’GeeksforGeeks’, ’is’, ’an’, ’awesome’, ’website’]
Line re. split (& # 39 ;, | _ | - |! & # 39 ;, data)
tells Python to split the variable’s data into characters: either _ or — or! , The character “ | Represents or.
There are several characters in a regular expression that are treated as special characters and have different functions. If you want to strip such a character, you need to escape it using " / " (backslash). Here is a list of special characters to escape before using them:
. + *? [^] $ () {} =! | : -
For example :
|
Exit:
[’GeeksforGeeks’,’ is’, ’an’,’ awesome’ , ’app’,’ too’]
Note. To learn more about regular expressions, click here .
Using re.findall ()
This is a little more cryptic, but saves time. It also uses regex as described above, but instead of the .split()
method, it uses a method named .findall ()
. This method finds all matching instances and returns each one in the list. This delimiting method is best used when you don’t know exactly what characters you want to delimit.
|
Exit:
The original string is: This, is - another: example ?!
The list after performing split functionality: [’ This’, ’is’,’ another ’,’ example ’]
Here the keyword [/ w & # 39;] +
indicates that it will find everything instances of alphabets or will underline (_) one or more and return them in a list.
Note: [/ w & # 39;] +
is not split into underscores ( _ ) as it looks for alphabets and underscores ...
For example :
|
Exit :
[’This’,’ is’, ’underscored’,’ _’, ’example’]
Using replace () and split ()
This is a very new way to split. It does not use regular expressions and is inefficient, but still worth a try. If you know the characters you want to split characters into, just replace them with a space and then use .split()
:
|
Exit:
The original string is: Let’s_try, this now
The list after split functionality: [“Let’s performing”, ’t ry ’,’ this’, ’now’]
Character Classes
Regex Character Description Cheat Sheet
Shorthand character class | Represents |
---|---|
d | Any numeric digit from 0 to 9 |
D | Any character that is not a numeric digit from 0 to 9 |
w | Any letter, numeric digit, or the underscore character |
W | Any character that is not a letter, numeric digit, or the underscore character |
s | Any space, tab, or newline character |
S | Any character that is not a space, tab, or newline |