regex - Python - Modifying a backreference. Can it be done? -
New in Python, so please forgive my ignorance. I am trying to modify the strings referenced back in a regular expression.
Example:
& gt; & Gt; & Gt; A_string 'fsa fad fdsa dsafasdf u.s.a. U.S.A. U.s.a fdas adfs.f fdsa f.afda '& gt; & Gt; & Gt; Re.sub (r '? (. & Lt;.? = \ S) (([a-zA-Z] \) + [a-zA-Z] \ {0,1}) (= \ s) , '& Lt; acronym & gt;' + re.sub (r '\.,' R '', ('r' ''). Upper ()) '' , a_string ) 'Fsa fad fdsa dsafasdf & lt; Acronyms & gt; USA & lt; / Acronyms & gt; & Lt; Acronyms & gt; United States & lt; / Acronyms & gt; & Lt; Acronyms & gt; U.s.a & lt; / Acronyms & gt; Fdas adfs.f fdsa f.afda ' Instead, I think the desire for output is:
' fsa fadsa dsafasdf & lt; Acronym & gt; USA & lt; / Acronym & gt; & Lt; Acronyms & gt; United States & lt; / Acronyms & gt; & Lt; Acronyms & gt; United States & lt; / Acronyms & gt; thanks for your help.
>
from:
If repl is a function, it is called for each non-overlapping event of pattern function takes a single match object argument, and the replacement Returns the string. For example: See examples in the link and Docs.
Comments
Post a Comment