Regex pattern for searches with include and exclude

Posted by alex-kravchenko-zmeyp on Stack Overflow See other posts from Stack Overflow or by alex-kravchenko-zmeyp
Published on 2010-03-14T21:57:05Z Indexed on 2010/03/14 22:05 UTC
Read the original article Hit count: 310

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about regex