简体   繁体   中英

Python RegEx search and replace with part of original expression

I'm new to Python and looking for a way to replace all occurrences of "[AZ]0" with the [AZ] portion of the string to get rid of certain numbers that are padded with a zero. I used this snippet to get rid of the whole occurrence from the field I'm processing:

import re
def strip_zeros(s):
    return re.sub("[A-Z]0", "", s)

test = strip_zeros(!S_fromManhole!)

How do I perform the same type of procedure but without removing the leading letter of the "[AZ]0" expression?

Thanks in advance!

Use backreferences.

http://www.regular-expressions.info/refadv.html "\\1 through \\9 Substituted with the text matched between the 1st through 9th pair of capturing parentheses."

http://docs.python.org/2/library/re.html#re.sub "Backreferences, such as \\6, are replaced with the substring matched by group 6 in the pattern."

Untested, but it would look like this:

return re.sub(r"([AZ])0", r"\\1", s)

Placing the first letter inside a capture group and referencing it with \\1

you can try something like

In [47]: s = "ab0"

In [48]: s.translate(None, '0')
Out[48]: 'ab'

In [49]: s = "ab0zy"

In [50]: s.translate(None, '0')
Out[50]: 'abzy'

I like Patashu's answer for this case but for the sake of completeness, passing a function to re.sub instead of a replacement string may be cleaner in more complicated cases. The function should take a single match object and return a string.

>>> def strip_zeros(s):
...     def unpadded(m):
...             return m.group(1)
...     return re.sub("([A-Z])0", unpadded, s)
...
>>> strip_zeros("Q0")
'Q'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM