While searching mail in Google, we use the sytax like
from:devcoder hasattachments:true mySearchString on:11-aug
or
mySearchString from:devcoder on:11-aug anotherSearchKeyword
After parsing, I should get the keyvalue pair such as (from, devcoder), (on, 11-aug). What is the best way to implement this parsing in c#.
First
Split()
on space, then you have an array containing all search terms. Then you loop over them to find the ones thatContains()
a colon (:) andSplit()
those again on the colon.To Linq-ify Jason's answer:
Split by space, then for each component of the split, split it by
:
. Then proceed accordingly. Roughly:Output:
Output from your second string:
Here is one regular expression-based approach I have used in the past; it supports prefixes in combination with quoted strings.
A more correct/robust/performant approach would involve writing a simple parser, however in most usage scenarios the time and effort associated with implementing and testing the parser would be vastly disproportionate to the gains.
Output: