Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to escape hex digits in a regular expression!!!

Reply
Thread Tools

How to escape hex digits in a regular expression!!!

 
 
aefxx
Guest
Posts: n/a
 
      12-17-2004
Hi everybody.

I just wanted to leave a note on regular expression in java.
In one of my projects I had to match hex digits in a string.

Say, u want to search for 0x00 in a string, u would likely
search for the escape sequence in the API and find "\xhh"
with hh being the digits to search for.

Well, if done like this u would get an error, being told
that \x is not a valid escape sequence.
Actually u dont have to escape the x but the backslash.

So, it should look like this in the end: "\\x00".
This works fine for me. Hope I saved some people some
head scratching.

c ya
 
Reply With Quote
 
 
 
 
Virgil Green
Guest
Posts: n/a
 
      12-17-2004
aefxx wrote:
> Hi everybody.
>
> I just wanted to leave a note on regular expression in java.
> In one of my projects I had to match hex digits in a string.
>
> Say, u want to search for 0x00 in a string, u would likely
> search for the escape sequence in the API and find "\xhh"
> with hh being the digits to search for.
>
> Well, if done like this u would get an error, being told
> that \x is not a valid escape sequence.
> Actually u dont have to escape the x but the backslash.
>
> So, it should look like this in the end: "\\x00".
> This works fine for me. Hope I saved some people some
> head scratching.


Actually, you are escaping the second \ with the first to make a single \ in
the java String. The single \ thus created is escaping the x so as to
indicate a hex value withing the regex.

- Virgil


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
\x used with no following hex digits abhi147@gmail.com C Programming 14 08-14-2006 06:59 AM
Hex digits to special chars abhi147@gmail.com C Programming 8 08-04-2006 05:37 AM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
4 digits hex (WORD) Magix C Programming 2 06-15-2004 01:19 AM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments