简体   繁体   中英

Creating a regular expression validation that limits the responses to only certain words (non case specific)?

I am writing google form validation (regular Expression). The validation is the answer must contain either Spring, Summer, Fall, Winter and the year. I also am trying to make it so it is not case sensitive. Currently, this what I have written. I have tried numerus other ways to write this and can not get it to work. Google forms allows you to select Contains, Doesn't contain, Matches, Doesn't Match

I have selected Contains and the pattern is ([A-Za-z]Spring|Fall|Summer|Winter)+[2]+[0]+[2-9]+[0-9]

So when a person enter spring, Spring, SPriNg, SPRING and a year the validation accepts it but it wont except a non season and year.

You could write a verbose pattern to match all case insensitive variations, and you could write the year part like 20[2-9][0-9]

^(?:[sS][pP][rR][iI][nN][gG]|[fF][aA][lL][lL]|[sS][uU][mM][mM][eE][rR]|[wW][iI][nN][tT][eE][rR])20[2-9][0-9]$

Regex101 demo

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