tags:

views:

816

answers:

7

Hello,

I am looking for a regular expression to determine when any of the values in a 32-bit hex value is non-zero.

The data patterns look like 0x00000000 and I want to know when any of the digits is non-zero. For example, if 0x00001000 or 0x10000000 or 0xB000000 would be capture by the regular expression, but not a 0x00000000 pattern. Right now I perform a walking pattern match of

0x[^0]
0x0[^0]
0x00[^0]
...
0x0000000[^0]

This will work, but I much rather have one pattern if possible. Thanks.

Mark

Edit: I didn't mention as the RegEx was not needed in a program, otherwise I would have used a different approach, but I was using the RegEx to search for values in a log file using UltraEdit. I could have developed a program or some other means to search, but I was just being lazy, just being honest. Ben S solution worked both in UltraEdit and Rad Software Regular Expression Designer. rampion solution didn't work in either tool, not sure why.

+4  A: 

Why not test the hex value against zero? Simpler, faster, more readable.

If a regular expressiong is really necessary, 0x0*[1-9a-fA-F][0-9a-fA-F]* should do it.

It looks for as many zeros as it can until it finds a non-zero hex value, then gathers the rest of the hex regardless of if it's a zero or not.

Note: this will match any length hex, not just 32 bits.

Ben S
this wouldn't allow zeros in the middle .. like 0x101
Aziz
Of course it would. No one said that the matched string has to end with the pattern. The point is that the last 0* is superfluous.
Svante
my point is that 0x101 will only return 0x10 ... Not very sure that this is what lordhog wants ;)
Aziz
Edited to fix the middle zero issue.
Ben S
It's now similar to rampion's answer, which, I believe, is correct.
Aziz
A: 

Surely a simple string compare and if it DOES NOT EQUAL "0x00000000" you've got your match.

Am I over simplifying it? The is only one FALSE case, right? When the string is "0x00000000"?

Don't use RegEx unless you have to.

rjstelling
he wants non-fixed length, like 0x0 or 0x0000
Aziz
+1  A: 
/0x0*[^0]/
chaos
this will match the first list of zeros followed by a single non-zero ... something like 0x11 will return 0x1 only
Aziz
This would match a line like "0x0," I'm tempted to downvote.
OP didn't say he was matching against a line, he said he was matching against a hex number. If it were written for content in an arbitrary text line it would be different, yes.
chaos
+2  A: 
/0x0*[1-9a-fA-F][0-9a-fA-F]*/

<atom>* means match the atom 0 or more times, so this pattern matches the 0x prefix, followed by 0 or more 0s, followed by a non-zero hex, followed by some hex.

rampion
I think that last * shouldn't be there, you want to match at least one non-zero character, and you don't need to match any paste the first.
Chas. Owens
+2  A: 

Why not try something slighly different. Testing for a non-zero hex is much harder than testing for a zero hex. So test for zero and manually do the not.

bool IsNonZeroHex(string input) {
  return !Regex.IsMatch(input, "^0x(0*)$");
}
JaredPar
A: 

I think this should cover all cases (if it really has to be a regex):

^0x(?=0*[1-9a-fA-F]0*)[0-9a-fA-F]{8}$
dr Hannibal Lecter
Would this force 0-padding since it requires a length of 8 characters. What about 0xfd3? That's still a valid 32-bit hex value.
Ben S
Um, yes, I was focusing on the *exact* match. But since your solution does all that with a simpler regex, it doesn't matter anymore :)
dr Hannibal Lecter
A: 
/0x0{0,7}[^0]/

'0x', followed by zero to seven '0', followed by something that is not '0'

Brad Gilbert