tags:

views:

53

answers:

2

Hello all I'm trying to find all the 6 digit numbers in a string that do not end in 00. Something like that /([0-9]{4})([^00])/ //i know this is wrong

so the string

asdfs dfg_123456_adsagu432100jhasj654321

will give me

results=[123456,654321] and not 432100

Thanks
Have a good day

+1  A: 

You're misusing the character class.
Just like [ab] matches a orb, [00] matches 0 or 0. A single character class cannot match two characters at once.
[^00] matches a single non-zero character (such as a)

Instead, use the following:

/([0-9]{4})([0-9][1-9]|[1-9][0-9])/
SLaks
I don't think so. That'll match 1234ab.
Jefromi
Try to use `(?: )` to not include two chars to pattern: `/([0-9]{4})(?:[^0]{2})/`
SergeanT
Please test your answer here http://regex.larsolavtorvik.com/ with the string i gave you and you will see that is not producing the desirable result.
Argiropoulos Stavros
@Argiropoulos: I already fixed it; it works now.
SLaks
+7  A: 

Use negative lookbehind: \d{6}(?<!00)

See also

polygenelubricants
You could also use lookahead, I suppose - look forward to see if there's not a zero zero, then match two digits.
Jefromi
@Jefromi: I think lookbehind is most natural. It's "match this, but exclude that", instead of "match this much, then lookaround, then maybe match some more".
polygenelubricants
@polygenelubricants: Yeah, I agree, just throwing it out there. Would be more intuitive if there were a lot more to match after the excluded bit.
Jefromi