Python's approach to string segmentation and interception

Source: Internet
Author: User

to intercept a string we can use the split method, split is divided by different separators to splitnow we want to intercept the regular matched content. Let's take a look at split. How to implement string segmentation>>> b= ' AAA,BBB,CCC '>>> b.split (', ')[' AAA ', ' BBB ', ' CCC '] so we're going to get the AAA section, how do we interceptmethod One:>>> b.split (', ') [0]' AAA ' Method Two:We can use the RE module to split the string with group, of course we use () to group the>>> Re.search (' ([a-z]*), ([a-z]*), ([a-z]*) ', b)<_sre. Sre_match object at 0x17e67e8>>>> Re.search (' ([a-z]*), ([a-z]*), ([a-z]*) ', b). Group (0)' AAA,BBB,CCC '>>> Re.search (' ([a-z]*), ([a-z]*), ([a-z]*) ', b). Group (1)' AAA '>>> Re.search (' ([a-z]*), ([a-z]*), ([a-z]*) ', b). Group (2)' BBB '>>> Re.search (' ([a-z]*), ([a-z]*), ([a-z]*) ', b). Group (3)' CCC '

Python's approach to string segmentation and interception

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.