简体   繁体   中英

Regular expression must contain and may only contain

I want a regular expression for python that matches a string which must contain 4 digits, it may not contain any special character other than "-" or ".", and it may only contain uppercase letters. I know the following matches text with 4 digits or more. How would I add the rest of the criteria?

[0-9]{4,}

An example would be: ART-4.5-11 is good, ART5411 is good, 76543 is good, but aRT-4!5-11 is bad since it contains a lowercase char and a special char that is not "-" or "."

The pattern:

pattern = '^[A-Z.-]*(\d[A-Z.-]*){4,}$'
  • ^ - start of the word
  • [AZ.-]* - any number of optional non-digit "good characters": letters, periods or dashes
  • (\\d[AZ.-]*){4,} - 4 or more groups of a digit and other "good characters"; this part provides at least 4 digits
  • $ - end of the word

Examples:

re.match(pattern, "ART-4.5-11")
# <_sre.SRE_Match object; span=(0, 10), match='ART-4.5-11'>    
re.match(pattern, "ART5411")
# <_sre.SRE_Match object; span=(0, 7), match='ART5411'>
re.match(pattern, "aRT-4!5-11") # No match
re.match(pattern, "76543")
# <_sre.SRE_Match object; span=(0, 5), match='76543'>

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