1

This regex ^.*.*.*.*.*.*00 will return true for this number 10102001 with preg_match.

i know that will catch the first seven numbers, but if the last number is not 0, so, the result should be false and not true. This is what I need. For this number the regex will work as i expect (false): 10102011

10102001 -- true why?
10102011 -- false

What is wrong with the regex?

4

4 回答 4

5

.* will match zero or MORE of any character that is not a line break. Your regex would actually be equivalent to just writing ^.*00. It matches because there is the substring "1010200" in "10102001".

You will want something like: ^.{6}00 (which is the same as ^......00) to give you what you want. Better yet, you'd probably want to be more specific and specify that you only want numbers prefixed. So ^[0-9]{6}00. And if you what to make sure that the 0s at the end are at the very end of the line or target string, then add on a $. ^.{6}00$

于 2013-03-04T17:44:38.970 回答
2

10102001 -- true why?

Because you are not matching to the end of string so the regex will match anything with 00 somewhere before the end (anything may follow the zeroes). In addition, * means "zero or more", which means that .* matches anything - the empty string, ten characters, four characters, 1 character...

Here is a regex that catches everything which starts with 7 characters and ends in two zeroes (9 characters in total):

^.......00$
于 2013-03-04T17:42:30.313 回答
1

Your regex is all kinds of messed up. The last 5 .*s are pointless, ^.*00 would do the same thing.

10102001 is true because it's matching its substring, 1010200. It's leaving the 1 on the end off. If you want to check and see if it matches the entire string, use this: ^.*00$.

Here's what your regex is doing. Add a $ at the end to see how it should work.

http://www.regex101.com/r/wK3qI1

I can't make any recommendations for how your regex should be other than that, since you haven't posted any constraints.

于 2013-03-04T17:41:38.647 回答
0

this should be the right regex for you:

[0-9]{7}0$
于 2013-03-04T17:43:03.880 回答