The Python re module provides some flags, which are more like conditions that need to be satisfied when a certain operation is performed. It is an optional argument value which default value is 0 means no flags are raised. There is the last argument in split() method called flag. This type of splitting the string comes very handy when we want both the split string as well as the separator. In this output, you can see that by specifying the pattern as capturing group (\W+) not only split the string by matched pattern but also include the pattern. #split the string by non alphanumeric characters and include them in the list #pattern sequence for non-alphanumeric characters as capturing group But here we will enclose the \W+ in the capturing group such as (\W+). Let’s repeat the above example where we are splitting a string that is present in theįormat. , the split() method will also include the separator in the returning list. In the re.split() method if the regular expression pattern is defined in the capturing group or parentheses
How to split the string with the separator and include the separator as well? #split the string by non alphanumeric characters #pattern sequence for non-alphanumeric characters We could also perform the same tasks using r'\W+' pattern. In the above example the pattern r',|-' represents raw string expression for, or. #pattern sequence for hyphen or comma matching So in the pattern, we have to write such a regular expression that can match either comma or hyphen. In the given string there are two delimiters comma(,) and hyphen(-). Method, and it will return us a list with the required items. And we want to extract the information from this string in a list as Method we can split a string that has multiple separators or delimiters characters.įor instance, we have a string in this format With Python string’s split() method we can split a string by a fixed delimiter, but with the help of
How to split a string that has multiple delimiters characters? As - is a non-alphanumeric character \W+ matches it. In the above example, the \W+ represents the regular expression pattern for the non-Alphanumeric character.
#pattern sequence for hyphen or non alphanumeric chracterĭetail = re.split(pattern, string, maxsplit=2) Here we only want to split the string by the first two Hyphens. Let’s say we have a string that contains details in id-name-DD-MM-YY format, and we need to split it into a list as id, name, and DD-MM-YY. Let’s see theĪrgument in action with an example. But we can also set it to a positive integer value and limit the split number.Īrgument to 3, then only three splits will be performed on the string. Is 0 which means split the string by all the possible occurrence patterns. String = "Hello Welcome to TechGeekBuzz Python RegEX Tutorial"Īrgument, that can set a limit to the number of splits. #pattern sequence for multiple white space Matches the multiple white spaces in the string, and we will use it as a pattern to split our string into a list of words. Is regular expression matches the whitespaces in the string. This means we need to use the white space pattern as a separator for the re.split() method The To split a string into a list of words we have to separate the individual words by white space. Use the re.split() method to split a string into words. This simply means, for capturing parenthesis’s regular expression pattern, the separators are also included in the list. If we use the capturing group (parentheses) in the separator or pattern, then the separator group will also be included in the returned list.
If the specified regular expression is not found in the targeted string, the split() method returns a list containing the target string as a single element. Method returns a list of split substrings using the occurrence of the pattern as a separator. When we want to perform the ignore-case searching, or By default, its value is 0 means no flags are raised. It is also an optional argument value, that defines the raised flag on the split() method. By default its value is 0 means split all the occurrences by the pattern, we can also specify it as a positive integer value to limit the number of split occurrences. It is an optional argument value, that defines the number of split occurrences.