"Python Cookbook" "String and Text" 1. Splitting a string against any number of delimiters

Source: Internet
Author: User

Problem: Splitting a string that is inconsistent between delimiters (and spaces between delimiters) into different fields;

Solution: Use the more flexible re.split () method, which can specify multiple patterns for delimiters.

Description: Split () of a string object can handle only simple cases, and multiple separators are not supported, and there are no possible spaces around the delimiter.

#example.py##Example of splitting a string on multiple delimiters using a regexImportRe#Import Regular Expression module Line='asdf fjdk; afed, FJEK,ASDF, foo'#(a) splitting on space, comma, and semicolonParts = Re.split (r'[;, \s]\s*', line)Print(Parts)#(b) using capturing groups in regular expression patterns, be aware that capturing groups are enclosed in parentheses, using capturing groups to cause matching text to also be included in the final resultFields = Re.split (r'(; |,|\s) \s*', line)Print(Fields)#(c) Improved output of strings based on the separator character aboveValues = Fields[::2]delimiters= Fields[1::2]delimiters.append ("')Print('value =', values)Print('delimiters =', delimiters) newline="'. Join (V+d forV,dinchzip (values, delimiters))Print('newline =', newline)#(d) Use of non-capturing groups (?: ...) The formal implementation of the regular expression pattern is grouped with parentheses, and the delimiter is not outputParts = Re.split (r'(?:,|;| \s) \s*', line)Print(parts)
>>> ================================ RESTART ================================>>> ['asdf','FJDK','afed','Fjek','asdf','Foo']['asdf',' ','FJDK',';','afed',',','Fjek',',','asdf',',','Foo']value= ['asdf','FJDK','afed','Fjek','asdf','Foo']delimiters= [' ',';',',',',',',',"']newline=asdf fjdk;afed,fjek,asdf,foo['asdf','FJDK','afed','Fjek','asdf','Foo']>>>

"Python Cookbook" "String and Text" 1. Splitting a string against any number of delimiters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.