Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
603 views
in Technique[技术] by (71.8m points)

regex - Regular Expression matches more than it should

This question is intended to be a target for closing similar questions as a duplicate.

I have written a regular expression that seems to be mostly working, except that it allows things in the input beyond the end of the expression.

Regex: /(d{4})-(d{2})-(d{2})/

Should match dates in ISO format, but it matches 2014-11-123456, 12342014-11-12 and even non-numbers like a1234-56-78z.

What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Regular expressions search for a pattern within the search string. This means that if the regex matches starting from anywhere, and ending anywhere, then it is considered valid. In the examples given:

2014-11-123456, 12342014-11-12 and a1234-56-78z all match the regular expression somewhere within them.

This is normal and expected behaviour. In your result set, you will have a total of four elements:

[0]: 2014-11-12  (whole match)
[1]: 2014        (first subpattern)
[2]: 11          (second subpattern)
[3]: 12          (third subpattern)

If you want to verify that the pattern matches your entire string, and only that string, you must anchor your regex using ^ (start of string) and $ (end of string). Your regex will become:

/^(d{4})-(d{2})-(d{2})$/

This will prevent any of your given examples from matching, because they do not match the start and end at the beginning and end of the string correspondingly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...