Reputation: 175
I have a string pattern intended to match to user utterances. I am struggling with match collection feature of regex.
string pat = ^(?=.*\b(?:goods?|things?|items?)\b)(?=.*\bbought\b)(?=.*\bid\b).*$
string user = "I would like to see id of bought goods.";
Correct matching utterance from user would be .i.e. I would like to see id of bought goods.
Problem:
Is there any possibility to use match collection feature in regex to determine if a user has only entered a part of required utterance i.e. I would like to see id of goods. (but missing bought)
I am required to detect the missing part of the pattern/match.
So that the user can be prompted and informed that he/she has missed bought in utterance.
Thanks in advance.
Upvotes: 1
Views: 654
Reputation: 41838
Yes, but... IndexOf
Yes, using a match collection or even a capture collection is technically possible here (but see the discussion below). For instance,
\b(?:(I|would|like|to|see|id|of|bought|goods)\b\s*)+
Will match the whole sentence in one pass, and the Capture Collection for Group 1 will include all the words that were present.
Alternately, you could use
\b(?:I|would|like|to|see|id|of|bought|goods)\b
to place all the matches in a match collection.
But then you'd have to inspect the match collection or the capture collection to see which word might be missing. With all that work, you'd be better off looking for each word separately (as @Jerry suggested) using IndexOf
Upvotes: 1