Python Removing last character _ from string using regex -
i know there bunch of other regex questions, hoping point out wrong regex. have done research , looks should work. used rubular test it, yes know regex ruby, same rules used should apply python looks in python docs
currently have
a = ["sdfsd_sfsdf234234","sdfsdf_sdfsdf_234324","tsfsd_sdf_213123"] c = [re.sub(r'[a-z]+', "", x) x in a]
which returns
['sdfsd_sfsdf', 'sdfsdf_sdfsdf_', 'tsfsd_sdf_']
but want return
['sdfsd_sfsdf', 'sdfsdf_sdfsdf', 'tsfsd_sdf']
i try use regex
c = [re.sub(r'$?_[^a-z_]+', "", x) x in a]
but getting error
traceback (most recent call last): file "<stdin>", line 1, in <module> file "/usr/lib64/python2.6/re.py", line 151, in sub return _compile(pattern, 0).sub(repl, string, count) file "/usr/lib64/python2.6/re.py", line 245, in _compile raise error, v # invalid expression
can me figure out doing wrong?
the error in:
c = [re.sub(r'$?_[^a-z_]+', "", x) x in a]
is caused ?
, not preceded characters doesn't know match 0 or 1 times. if change to:
>>> [re.sub(r'_?[^a-z_]+$', "", x) x in a] ['sdfsd_sfsdf', 'sdfsdf_sdfsdf', 'tsfsd_sdf']
it works expect.
another thing, $
used detonate end of line, shouldn't first character.
Comments
Post a Comment