0

I'm new to Python and looking for a way to replace all occurrences of "[A-Z]0" with the [A-Z] 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 "[A-Z]0" expression?

Thanks in advance!

1
  • 1
    Do you mean to have zeroes in your example? Commented Mar 13, 2013 at 5:06

3 Answers 3

2

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"([A-Z])0", r"\1", s)

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

Sign up to request clarification or add additional context in comments.

1 Comment

If I read the question right, the regex should be r"([A-Z])0".
0

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'

Comments

0

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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.