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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top