How can I split characters after replacing Python strings?

Source: Internet
Author: User

Python string replacement requires constant learning. Only continuous learning can be used better. Next, let's take a look at how to replace the Python string in actual use. This will help you in future use.

Python string replacement

Replace all matched substrings. Replace all substrings in the subject that match the regular expression regex with newstring

 
 
  1. Result, number = re. subn (regex, newstring, subject)
  2. Replace all matched substrings with regular expression objects)
  3. Rereobj = re. compile (regex)
  4. Result, number = reobj. subn (newstring, subject) string split

String splitting

 
 
  1. Reresult = re. split (regex, subject)
  2. String sharding uses regular expression objects)
  3. Rereobj = re. compile (regex)
  4. Result = reobj. split (subject) Match

The following lists the matching usage of Python Regular Expressions:

Test whether the regular expression matches all or part of a string.
 

 
 
  1. Regex = ur "..." # Regular Expression
  2. If re. search (regex, subject ):
  3. Do_something ()
  4. Else:
  5. Do_anotherthing ()

Test whether the regular expression matches the entire string.

 
 
  1. Regex = ur "... \ Z" # The regular expression ends with \ Z.
  2. If re. match (regex, subject ):
  3. Do_something ()
  4. Else:
  5. Do_anotherthing ()

Create a matching object and obtain matching details through the object.

 
 
  1. Regex = ur "..." # Regular Expression
  2. Match = re. search (regex, subject)
  3. If match:
  4. # Match start: match. start ()
  5. # Match end (exclusive): match. end ()
  6. # Matched text: match. group ()
  7. Do_something ()
  8. Else:
  9. Do_anotherthing ()

The above describes how to replace Python strings.

Related Article

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.