D
Dave
I'm struggling with something that should be fairly simple. I just don't
know the regext syntax very well, unfortunately.
I'd like to parse words out of what is basically a boolean search string.
It's actually the input string into a Microsoft Index Server search.
The string will consist of words, perhaps enclosed in quotes or parentheses.
I'd like to use Regex to pull out the words, or the phrases if the words are
enclosed in quotes. Example
The string: asdf or qwer or hjkl
should yield three results: asdf, qwer, hjkl
and:
"two words" and asdf
should yield two results: "two words", and "asdf"
There's the added complexity that the strings may have groups of words
surrounded by parentheses, but I think I can figure that out if I solve the
quoted strings problem.
I've tried a few things, but I can't manage to come up with something that
isn't returning the quotes in the return values.
Here's some code:
Regex regEx("") = new Regex("([\"][^\"]+[\"]|\\S+)");
string searchText = "\"two words\" and asdf";
foreach (Match m in regEx.Matches(searchText))
{
string text = m.ToString();
MessageBox.Show(text);
}
In the above code, it will pull out the words, but the text pulled out
includes the quotes in "two words";
I tried to tell it to match but ignore the quotes, using:
Regex regEx("") = new Regex("(?
\"){1}[^\"]?
\"){1}|\\S)+");
but that doesn't work either. Obviously I don't know what I'm doing.
Please help!
- Daev
know the regext syntax very well, unfortunately.
I'd like to parse words out of what is basically a boolean search string.
It's actually the input string into a Microsoft Index Server search.
The string will consist of words, perhaps enclosed in quotes or parentheses.
I'd like to use Regex to pull out the words, or the phrases if the words are
enclosed in quotes. Example
The string: asdf or qwer or hjkl
should yield three results: asdf, qwer, hjkl
and:
"two words" and asdf
should yield two results: "two words", and "asdf"
There's the added complexity that the strings may have groups of words
surrounded by parentheses, but I think I can figure that out if I solve the
quoted strings problem.
I've tried a few things, but I can't manage to come up with something that
isn't returning the quotes in the return values.
Here's some code:
Regex regEx("") = new Regex("([\"][^\"]+[\"]|\\S+)");
string searchText = "\"two words\" and asdf";
foreach (Match m in regEx.Matches(searchText))
{
string text = m.ToString();
MessageBox.Show(text);
}
In the above code, it will pull out the words, but the text pulled out
includes the quotes in "two words";
I tried to tell it to match but ignore the quotes, using:
Regex regEx("") = new Regex("(?


but that doesn't work either. Obviously I don't know what I'm doing.
Please help!
- Daev