简体   繁体   中英

python re.search quantifier

I have this:-

re.search("^[47]{2:}$", '447447')

... and was expecting it to return True. But somehow it does not.

How come? My understanding is that it was suppose to match any number which has any combination of 4 or 7, with at least 2 digits. Is that correct?

It should probably be "^[47]{2,}$" .

I visit the regular expression syntax page quite often, because I find it hard to remember all of the little tricks for building regexes.

The syntax is {m,n} where n could be omitted.

Fix:

re.search("^[47]{2,}$", '447447')

See RegEx syntax: https://docs.python.org/3/library/re.html#regular-expression-syntax

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM