On 24 October 2016 at 23:10, Paul Moore <p.f.mo...@gmail.com> wrote: > On 24 October 2016 at 21:54, Chris Barker <chris.bar...@noaa.gov> wrote: >> I don't know a way to do "remove every character except these", but someone >> I expect there is a way to do that efficiently with Python strings. > > It's easy enough with the re module: > >>>> re.sub('[^0-9]', '', 'ab0c2m3g5') > '0235' > > Possibly because there's a lot of good Python builtins that allow you > to avoid the re module when *not* needed, it's easy to forget it in > the cases where it does pretty much exactly what you want, or can be > persuaded to do so with much less difficulty than rolling your own > solution (I know I'm guilty of that...). > > Paul
Thanks, this would solve the task of course. However for example in the case in my last example (filenames) this would require: - Write a function to construct the expression for "all except given" characters from my table. This could be easy I believe, but still another task. Then: 1. Apply translate() with my table to the string. 2. Apply re.sub() to the string. I usually start using RE when I want to find/replace words or patterns, but not translate/filter the characters directly. So since there is already an "inclusive" translate() then probably having an "exclusive" one is not a bad idea. I believe it is something very similar in implementation, so instead of appending next character which is not in the table, it simply does nothing. Mikhail _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/