Reputation: 100
I'm trying to parse filters for a search command I'm coding. The user can search using one or more arguments. The values can have spaces (if so, must be wrapped around double quotes). Also, there are several "operators" I've declared, these are: =
(for equals), :=
(for starts with), =:
(for ends with) and *
(for contains).
For example:
name=John name:="John Doe" name*"ohn Do"
Must match: name=John
, name*"John Doe"
, name*"ohn Do"
I've tried using Regex (C#) but it won't match the string when it doesn't contain quotes. This is what I came up with so far:
Regex.Matches(input, "(name(=|:=|=:|\\*)([^\"](\\S+)) | (name(=|:=|=:|\\*)\"([^\"]*)\"))", RegexOptions.IgnoreCase);
Which is basically match the word "name" followed by one of the operators followed by anything up to a space, or the same but up to the closing double quotes. But it doesn't match anything.
In reality I'm replacing "name" with a few keywords inside a foreach
, but for the sake of simplicity, let's just match the word "name".
Anyway, any ideas what I might be doing wrong?
Upvotes: 0
Views: 197
Reputation: 26
The main problem was that you had spaces between your |
-operator and the capturing groups, removing those makes your regex match successfully against your example.
Your regex would fail when more spaces are used inside the quotes or when more then one space is used after one of your custom operators.
Regex.Match(input, @"(?:name(=|:=|=:|\*)((?:"(?:[\w\s]*)")|(?:[^"\s]*)))", RegexOptions.IgnoreCase);
This expression should work regardless of spaces and gives you both the custom operator that was used and the matched input, you'll have to sanitize the input if it starts and ends with quotes yourself for this particular expression. I've tested this expression with multiple spaces inside the quotes and between the operators and it behaved as I expected it, I've not checked anything else so be aware of that.
Upvotes: 1