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

A

aefxx

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
 
V

Virgil Green

aefxx said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top