> -----Original Message----- > From: RTO RTO [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, September 11, 2002 11:00 AM > To: [EMAIL PROTECTED] > Subject: Simple RegEx Question > > > Here is a RegEx that I am using to check if the given > string is Hexadecimal or not. > > /[^0-9a-fA-F]+/ #if this evals to true string is NOT > hex
This regex is true if there is a sequence of one or more characters that are not valid hex digits. > > I am having a trailing "+" to make sure at least one > permissible character is present. The + is not necessary. If there's one non-hex digit, that's enough to invalidate the string, right? > Yet, it matches an > empty string as a hex string. > > a) What am I missing? > b) Why is an empty string being matched? Your regex only matches if theres a non-hex digit. An empty string doesn't have any hex digits, so it doesn't match. Perhaps you should turn it around: /^[0-9a-fA-F]+\z/ This matches only if the entire string consists of a sequence of one or more valid hex digits. This will not treat an empty string as valid. The ^ (before the square bracket) anchors to the beginning of the string, while \z anchors to the end of the string. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]