Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #83694 > unrolled thread
| Started by | Shambhu Rajak <Shambhu.Rajak@sandisk.com> |
|---|---|
| First post | 2015-01-13 13:40 +0000 |
| Last post | 2015-01-14 14:42 +0100 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
what would be the regular expression for null byte present in a string Shambhu Rajak <Shambhu.Rajak@sandisk.com> - 2015-01-13 13:40 +0000
Re: what would be the regular expression for null byte present in a string Denis McMahon <denismfmcmahon@gmail.com> - 2015-01-13 17:25 +0000
Re: what would be the regular expression for null byte present in a string Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-14 14:42 +0100
| From | Shambhu Rajak <Shambhu.Rajak@sandisk.com> |
|---|---|
| Date | 2015-01-13 13:40 +0000 |
| Subject | what would be the regular expression for null byte present in a string |
| Message-ID | <mailman.17674.1421159068.18130.python-list@python.org> |
[Multipart message — attachments visible in raw view] — view raw
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. Thanks, Shambhu ________________________________ PLEASE NOTE: The information contained in this electronic mail message is intended only for the use of the designated recipient(s) named above. If the reader of this message is not the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify the sender by telephone or e-mail (as shown above) immediately and destroy any and all copies of this message in your possession (whether hard copies or electronically stored copies).
[toc] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-01-13 17:25 +0000 |
| Message-ID | <m93kdn$qlb$1@dont-email.me> |
| In reply to | #83694 |
On Tue, 13 Jan 2015 13:40:52 +0000, 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.
What have you tried, and what was the result?
Regex isn't designed to work with byte strings.
>>> str = '\x01\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x0010232ae8944a
\x02\x00\x00\x00\x00\x00\x00\x00\n'
>>> str.replace('\x00','').replace('\x0c','').replace('\x01','').replace
('\x02','').replace('\n','')
'10232ae8944a'
This works for the specific example you gave, will your "string" ever
contain unwanted characters apart from \x00, \x01, \x02, \x0c, \n, and is
it ever possible for one of those to be in the wanted set?
>>> str[12:24]
'10232ae8944a'
This also works for the specific example you gave, is the data you want
to extract always going to be at the same offset in the string, and of
the same length?
>>> ''.join([str[x] for x in range(len(str)) if str[x] >= ' ' and str[x]
<= '~'])
'10232ae8944a'
This also works for the specific example you gave, and is a way to remove
non printing and 8bit characters from a string. Is this what you actually
want to do?
>>> str.strip('\x00\x0c\x01\x02\n')
'10232ae8944a'
This also works for the specific example that you gave, it uses the strip
function with a string of characters to be stripped, this will work as
long as you can predefine all the characters to strip and none of the
characters to strip is ever desired as part of the result.
So 4 different methods, each of which seems to do, in the case of the
specific example you gave, exactly what you want.
However although I tried a few patterns, I don't seem to be able to
create an re that will do the job.
eg:
>>> patt = re.compile(r'[0-9a-zA-Z]+')
>>> res = patt.match(str)
>>> res
>>> print res
None
>>> type(res)
<type 'NoneType'>
>>> patt = re.compile(r'[0-z]+')
>>> res = patt.match(str)
>>> res
>>> print res
None
>>> type(res)
<type 'NoneType'>
>>>
>>> patt = re.compile(r'[ -~]+')
>>> res = patt.match(str)
>>> res
>>> print res
None
>>> type(res)
<type 'NoneType'>
>>>
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2015-01-14 14:42 +0100 |
| Message-ID | <18707534.kH9cM8qlhB@PointedEars.de> |
| In reply to | #83694 |
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 You need a character class with that range. > Can anyone help me with a working regex for it. Yes. > ________________________________ > > PLEASE NOTE: The information contained in this electronic mail message is > intended only for the use of the designated recipient(s) named above. […] Please disable this nonsense, do not post multi-part messages, or if this is not possible with your e-mail client or provider, use another one, respectively. -- 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