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
- Result, number = re. subn (regex, newstring, subject)
- Replace all matched substrings with regular expression objects)
- Rereobj = re. compile (regex)
- Result, number = reobj. subn (newstring, subject) string split
String splitting
- Reresult = re. split (regex, subject)
- String sharding uses regular expression objects)
- Rereobj = re. compile (regex)
- 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.
- Regex = ur "..." # Regular Expression
- If re. search (regex, subject ):
- Do_something ()
- Else:
- Do_anotherthing ()
Test whether the regular expression matches the entire string.
- Regex = ur "... \ Z" # The regular expression ends with \ Z.
- If re. match (regex, subject ):
- Do_something ()
- Else:
- Do_anotherthing ()
Create a matching object and obtain matching details through the object.
- Regex = ur "..." # Regular Expression
- Match = re. search (regex, subject)
- If match:
- # Match start: match. start ()
- # Match end (exclusive): match. end ()
- # Matched text: match. group ()
- Do_something ()
- Else:
- Do_anotherthing ()
The above describes how to replace Python strings.