Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   How to escape hex digits in a regular expression!!! (http://www.velocityreviews.com/forums/t139358-how-to-escape-hex-digits-in-a-regular-expression.html)

aefxx 12-17-2004 02:18 PM

How to escape hex digits in a regular expression!!!
 
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

Virgil Green 12-17-2004 10:12 PM

Re: How to escape hex digits in a regular expression!!!
 
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




All times are GMT. The time now is 12:11 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.