Home > other > How to use regular expressions are a small part of the replacement string
How to use regular expressions are a small part of the replacement string
Time:01-09
The following string, such as "12345 a67890", "Q345B678", "V666C888", "z111D222 "Common law is that there is a letter, there are two Numbers, before and after the letter Now you need to replaced all the letter "A" How to use the re. Sub write a regular expression
CodePudding user response:
Assume that a string of other position will no longer appear similar structures
CodePudding user response:
import re S=[" 12345 a67890 Q345B678 ", ""," V666C888 ", "z111D222"] Pat=r '(? <=\ d \ d) [a zA - Z] (?=\ d \ d) ' Result=[re sub (pat, 'A', I) for I s] in Print (result) # [' 12345 a67890 ', 'Q345A678', 'V666A888', 'z111A222]
CodePudding user response:
Regular: [a zA - Z] represents a letter, can use re. The replace () to replace,
CodePudding user response:
#! The/usr/bin/python The import re
Def re_change (TMP) : A=TMP group (0) B=re. Sub (' ([a zA - Z]) ', 'a', a) Return b
A={" az1234C32434RZ as3100P1234zx ", ""," 566 a3423 ", "Z3456M2334K"} For k in a: Print (' old: 'k) C=re. Sub (' (\ d {2} [a zA - Z] \ d {2}) ', re_change, k) Print (' new: ', c)