jqning
jqning

Reputation: 185

I would like to use regex to create a pattern that matches stems of three words, regardless of their order

I am using Blackboard to create a test. One of the questions has as the answer: TIF, PDF, or Native. Blackboard has the ability to use pattern matching to validate answers for fill-in-the-blank questions.

What regex will accept an answer containing all these words, regardless of the order and ignoring if a student puts an "and" in the answer?

Possible correct answers:

TIF PDF Native  
Tifs, Pdfs, and natives  
native and pdf and tif  
pdf, tifs  natives
tifs with text and metadata, PDF, Native

I need all three words, but I can ignore extra words.

I will quality control the answers, so if someone submits: tifs text pdf native excel image hard drive server, I will catch it manually.

Upvotes: 1

Views: 144

Answers (1)

Bohemian
Bohemian

Reputation: 425083

Use a look ahead for each word:

^(?i)(?=.*\btifs?\b)(?=.*\bpdfs?\b)(?=.*\bnatives?\b).*

See live demo.

Because look aheads don't consume input, their order and the order of the terms in the input are flexible.

Each term is wrapped in \b (word boundary) to ensure they are "words", eg "stiff pdf native" wouldn't match.

s? means an optional "s" at the end of the words (to allow plurals)

(?i) means "case insensitive".

Upvotes: 1

Related Questions