Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #83698 > unrolled thread
| Started by | Peter Otten <__peter__@web.de> |
|---|---|
| First post | 2015-01-13 15:41 +0100 |
| Last post | 2015-01-14 15:11 +0100 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: what would be the regular expression for null byte present in a string Peter Otten <__peter__@web.de> - 2015-01-13 15:41 +0100
Re: what would be the regular expression for null byte present in a string Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-14 14:52 +0100
Re: what would be the regular expression for null byte present in a string Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-14 15:11 +0100
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2015-01-13 15:41 +0100 |
| Subject | Re: what would be the regular expression for null byte present in a string |
| Message-ID | <mailman.17677.1421160126.18130.python-list@python.org> |
Shambhu Rajak wrote: > I have a string that I get as an output of a command as: > '\x01\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x0010232ae8944a\x02\x00\x00\x00\x00\x00\x00\x00\n' > > I want to fetch '10232ae8944a' from the above string. > > I want to find a re pattern that could replace all the \x01..\x0z to be > replace by empty string '', so that I can get the desired portion of > string > > Can anyone help me with a working regex for it. I think you want the str.tranlate() method rather than a regex. Assuming you are using Python 2: >>> delenda = "".join(map(chr, range(32))) >>> identity = "".join(map(chr, range(256))) >>> '\x01\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x0010232ae8944a\x02\x00\x00\x00\x00\x00\x00\x00\n'.translate(identity, delenda) '10232ae8944a' With Python3: >>> mapping = dict.fromkeys(range(32)) >>> '\x01\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x0010232ae8944a\x02\x00\x00\x00\x00\x00\x00\x00\n'.translate(mapping) '10232ae8944a'
[toc] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2015-01-14 14:52 +0100 |
| Message-ID | <13414923.1GtqDO6yAl@PointedEars.de> |
| In reply to | #83698 |
Peter Otten wrote: > Shambhu Rajak wrote: >> I have a string that I get as an output of a command as: >> > '\x01\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x0010232ae8944a\x02\x00\x00\x00\x00\x00\x00\x00\n' >> >> I want to fetch '10232ae8944a' from the above string. >> >> I want to find a re pattern that could replace all the \x01..\x0z to be >> replace by empty string '', so that I can get the desired portion of >> string >> >> Can anyone help me with a working regex for it. > > I think you want the str.tranlate() method rather than a regex. Another possibility, but given the length of the list probably not the most efficient one. Aside from re and this one, here is another (tested with Python 3.4.2): filtered = ''.join(filter(lambda ch: ord(ch) > 0x0f, s)) -- PointedEars Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2015-01-14 15:11 +0100 |
| Message-ID | <2003101.I8trXTtxXO@PointedEars.de> |
| In reply to | #83755 |
Thomas 'PointedEars' Lahn wrote: > Peter Otten wrote: >> Shambhu Rajak wrote: >>> I want to find a re pattern that could replace all the \x01..\x0z to be >>> replace by empty string '', so that I can get the desired portion of >>> string >>> >>> Can anyone help me with a working regex for it. >> >> I think you want the str.tranlate() method rather than a regex. > > Another possibility, but given the length of the list probably not the > most efficient one. Aside from re and this one, here is another (tested > with Python 3.4.2): > > filtered = ''.join(filter(lambda ch: ord(ch) > 0x0f, s)) filtered = ''.join(filter(lambda ch: ch > "\x0f", s)) -- PointedEars Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web