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)

CodePudding user response:

refer to the second floor rrrr336 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]


The regular expression is, thank you, please? <=and?=what said, I am in my regular expressions can't find the corresponding content in the teaching material, would you please advise, or recommend books or information

CodePudding user response:

reference 4 floor kk1924 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)

thank you

CodePudding user response:

refer to 7th floor weixin_41224699 response:
Quote: refer to 4th floor kk1924 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)

Thank you


Can I watch, later have what problem can I contact

CodePudding user response:

refer to the eighth floor kk1924 response:
Quote: refer to 7th floor weixin_41224699 response:
Quote: refer to 4th floor kk1924 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)

Thank you


Can watch me, later have what problem can I exchange

good
  • Related