Python splits strings for any number of delimiters (code attached)

不言
Release: 2018-11-27 15:50:15
forward
3060 people have browsed it

The content this article brings to you is about Python splitting strings (with code) for any number of delimiters. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .

1. Requirements

We need to split the string into different fields, but the delimiters (and the spaces between the delimiters) are in the entire string Not consistent.

2. Solution

The split() method of the string object can only handle very simple situations, and does not support multiple delimiters. There is nothing you can do about the space situation. When some more flexible functions are needed, the re.split() method should be used:

import re line='abc def ; ghi, jkl,mno, pkr' #分隔符:分号,都逗号,空格符,前后可以跟着任意数量的额外空格 result=re.split(r'\s*[;,\s]\s*',line) print(result)
Copy after login

Result:

['abc', 'def', 'ghi', 'jkl', 'mno', 'pkr']
Copy after login
Copy after login

3. Analysis

re.split() is useful because multiple patterns can be specified for separators. For example, in the above solution, the delimiters are: semicolon, comma, space, and can be followed by any number of additional spaces. Like the result obtained by str.split(), the final result is a field list.

When using re.split(), you need to be careful whether the capturing group in the regular expression pattern is included in parentheses.

If a capturing group is used, the matching text will also be included in the final result. For example, consider the following case:

import re line='abc def ; ghi, jkl,mno, pkr' result=re.split(r'\s*(;|,|\s)\s*',line) print(result)
Copy after login

Result:

['abc', ' ', 'def', ';', 'ghi', ',', 'jkl', ',', 'mno', ',', 'pkr']
Copy after login

It may also be useful to obtain the delimiter in a specific context. For example, to improve the output of a string with a delimiter character:

import re line='abc def ; ghi, jkl,mno, pkr' result=re.split(r'\s*(;|,|\s)\s*',line) values=result[::2] delimiters=result[1::2]+[''] print(values) print(delimiters) last=''.join(v+d for v,d in zip(values,delimiters)) print(last)
Copy after login

Result:

['abc', 'def', 'ghi', 'jkl', 'mno', 'pkr'] [' ', ';', ',', ',', ',', ''] abc def;ghi,jkl,mno,pkr
Copy after login

If you don't want to see delimiter characters in the results, but still want to use symbols for regex patterns Grouping, please make sure to use a non-capturing group, specified in the form of (?:...). Examples are as follows:

import re line='abc def ; ghi, jkl,mno, pkr' result=re.split(r'\s*(?:;|,|\s)\s*',line) print(result)
Copy after login

Result:

['abc', 'def', 'ghi', 'jkl', 'mno', 'pkr']
Copy after login
Copy after login

The above is the detailed content of Python splits strings for any number of delimiters (code attached). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!