RegEx for Hex string validation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • slg

    #1

    RegEx for Hex string validation

    How can i validate the characters in a string are all hex chars.

    I tried following but it does not work.

    Regex r = new Regex(@"^([A-F]|[a-f]|[0-9])*");



    TIA.

  • Kerem Gümrükcü

    #2
    Re: RegEx for Hex string validation

    Hi,

    what about trying to convert the hex into a e.g. long value?
    When it fails you will get a exception and you can handle the
    exception depending on the error.

    Regards

    Kerem

    --
    -----------------------
    Beste Grüsse / Best regards / Votre bien devoue
    Kerem Gümrükcü
    Microsoft Live Space: http://kerem-g.spaces.live.com/
    Latest Open-Source Projects: http://entwicklung.junetz.de
    -----------------------
    "This reply is provided as is, without warranty express or implied."


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: RegEx for Hex string validation

      slg <slg@abc.comwro te:
      How can i validate the characters in a string are all hex chars.
      >
      I tried following but it does not work.
      >
      Regex r = new Regex(@"^([A-F]|[a-f]|[0-9])*");
      You've got the ^ at the beginning, but no $ at the end. You can also
      express the middle part more simply:

      Regex r = new Regex(@"^[A-Fa-f0-9]*$");

      Note that it will match an empty string as well as one with actual
      content - is that what you want?

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      World class .NET training in the UK: http://iterativetraining.co.uk

      Comment

      • slg

        #4
        Re: RegEx for Hex string validation

        thx alot

        Yes, this is exactly what i was looking for.



        "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
        news:MPG.21bbbb e054733c2368d@m snews.microsoft .com...
        slg <slg@abc.comwro te:
        > How can i validate the characters in a string are all hex chars.
        >>
        >I tried following but it does not work.
        >>
        >Regex r = new Regex(@"^([A-F]|[a-f]|[0-9])*");
        >
        You've got the ^ at the beginning, but no $ at the end. You can also
        express the middle part more simply:
        >
        Regex r = new Regex(@"^[A-Fa-f0-9]*$");
        >
        Note that it will match an empty string as well as one with actual
        content - is that what you want?
        >
        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        World class .NET training in the UK: http://iterativetraining.co.uk

        Comment

        Working...