Regex pattern for searches with include and exclude
- by alex-kravchenko-zmeyp
I am working on a Regex pattern for searches that should allow optional '+' sign to include in the search and '-' sign to exclude from the search. For example: +apple orange -peach should search for apples and oranges and not for peaches. Also the pattern should allow for phrases in double quotes mixed with single words, for example: "red apple" -"black grape" +orange - you get the idea, same as most of the internet searches.
So I am running 2 regular expressions, first to pick all the negatives, which is simple because '-' is required:
(?<=[\-]"?)((?<=")(?<exclude>[^"]+)|(?<exclude>[^\s,\+\-"]+))
And second to pick positives, and it is a little more complex because '+' is optional:
((?<=[\+\s]")(?<include>[^\s"\+\-][^"]+))|(?<include>(?<![\-\w]"?)([\w][^,\s\-\+]+))(?<!")
Positive search is where I am having a problem, it works fine when I run it in RegexBuddy but when I try in .Net the pattern picks up second word from negative criteria, for example in -"black grape" it picks up word 'grape' even though it ends with double quote.
Any suggestions?