Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to split a string for any number of delimiters in Python

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how to split strings for any number of delimiters in Python. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

We need to split the string into different fields, but the delimiter (and the space between the delimiters) is not consistent throughout the string.

The split () method of a string object can only handle very simple cases, does not support multiple delimiters, and is powerless to deal with multiple spaces around the delimiter. When you need some more flexible functionality, you should use the re.split () method:

Import reline='abc def; ghi, jkl,mno, pkr'# delimiter: semicolon, all comma, space character, can be followed by any number of extra spaces result=re.split (r'\ s * [;,\ s]\ s) print (result)

Results:

['abc',' def', 'ghi',' jkl', 'mno',' pkr']

Re.split () is useful because you can specify multiple modes for the delimiter. For example, in the above solution, delimiters: semicolons, all commas, spaces, can be followed by any number of extra spaces. As with str.split (), the final result is a list of fields.

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

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

Import reline='abc def; ghi, jkl,mno, pkr'result=re.split (r'\ s * (; |, |\ s)\ print (result)

Results:

['abc',', 'def',';', 'ghi',' jkl', 'mno',', 'pkr']

It may also be useful to get delimiters in a particular context. For example, use delimiters to improve the output of a string:

Import reline='abc def; ghi, jkl,mno, pkr'result=re.split (r'\ s * (;; |, |\ s)\ senvelope grammar line) values=result [:: 2] delimiters= result [1:: 2] + ['] print (values) print (delimiters) last=''.join (volud for v in zip (values,delimiters)) print (last)

Results:

['abc',' def', 'ghi',' jkl', 'mno',' pkr'] [',';','] abc def;ghi,jkl,mno,pkr

If you do not want to see delimiters in the results, but still want to group regular expression patterns with symbols, be sure to use non-capture groups to (?: …) Specifies the form of the. Examples are as follows:

Import reline='abc def; ghi, jkl,mno, pkr'result=re.split (r'\ s * (?:;; |, |\ s)\ print (result)

Results:

['abc',' def', 'ghi',' jkl', 'mno',' pkr' so much about how to split strings for any number of delimiters in Python. I hope the above can help you and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report