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

Discussion in 'Java' started by aefxx, Dec 17, 2004.

  1. aefxx

    aefxx Guest

    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
    aefxx, Dec 17, 2004
    #1
    1. Advertising

  2. aefxx

    Virgil Green Guest

    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
    Virgil Green, Dec 17, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,269
  2. Replies:
    10
    Views:
    6,135
    Neredbojias
    Aug 19, 2005
  3. Bengt Richter
    Replies:
    6
    Views:
    448
    Juha Autero
    Aug 19, 2003
  4. Abanowicz Tomasz

    regular expression for digits

    Abanowicz Tomasz, Nov 15, 2007, in forum: Perl Misc
    Replies:
    5
    Views:
    67
    Dr.Ruud
    Nov 19, 2007
  5. lev
    Replies:
    9
    Views:
    123
Loading...

Share This Page