Splitting Strings with Multiple Delimiters in Python
Many tasks in programming involve manipulating strings. When working with strings containing multiple delimiters, such as semicolons and commas, splitting them efficiently is essential.
Using Regular Expressions
One common approach is to use regular expressions. Consider the example string:
"b-staged divinylsiloxane-bis-benzocyclobutene [124221-30-3], mesitylene [000108-67-8]; polymerized 1,2-dihydro-2,2,4- trimethyl quinoline [026780-96-1]"
To split this string by either a semicolon or a comma followed by a space, we can use the following regular expression:
'; |, '
This pattern matches either a semicolon or a comma followed by a space. Using the re.split() function, we can split the string as follows:
import re re.split('; |, ', "b-staged divinylsiloxane-bis-benzocyclobutene [124221-30-3], mesitylene [000108-67-8]; polymerized 1,2-dihydro-2,2,4- trimethyl quinoline [026780-96-1]")
Note: Individual commas without trailing spaces will not be split.
Update: Extended Delimeter Pattern
The given solution can be further extended to include additional delimiters. For instance, to split the following string by semicolons, commas, asterisks, and newlines:
"Beautiful, is; better*than\nugly"
The regular expression would become:
'; |, |\*|\n'
Using the re.split() function as before:
re.split('; |, |\*|\n', 'Beautiful, is; better*than\nugly')
This would result in the following split result:
['Beautiful', 'is', 'better', 'than', 'ugly']
The above is the detailed content of How Can I Split Strings with Multiple Delimiters in Python Using Regular Expressions?. For more information, please follow other related articles on the PHP Chinese website!