EDIT
Wie Sie im Kommentar erwähnt, sollte es auch mit verschachtelten Klammern arbeiten. Also änderte ich den Code zu folgenden:
List<string> output = new List<string>();
string input = "(\"always use\" OR \"bar\") OR (\"Hello\" AND \"market cost\")OR((\"IT\"AND\"P T\")AND(\"PO\"NOT\"pop good\"))";
var openSplit = input.Split('(');
for (int i = 0; i < openSplit.Length; i++)
{
if (openSplit[i] == "")
{
// put a '(' on
output.Add("(");
}
else
{
var closeSplit = openSplit[i].Split(')');
for (int j = 0; j < closeSplit.Length; j++)
{
var quoteSplit = closeSplit[j].Split('"');
foreach (var quote in quoteSplit)
{
if (quote != "")
{
output.Add(quote.Trim());
}
}
// put a ')' on the end, but not if it's the last one
if (j < closeSplit.Length - 1)
{
output.Add(")");
}
}
// put a '(' on the end, but not if it's the last one
if (i < openSplit.Length - 1)
{
output.Add("(");
}
}
}
Dies erzeugt folgende Ausgabe jeden Wert Druck von output
(
always use
OR
bar
)
OR
(
Hello
AND
market cost
)
OR
(
(
IT
AND
P T
)
AND
(
PO
NOT
pop good
)
)
alte Lösung nach Ihrer ersten Frage: Hier ist eine Lösung ohne Regex. Das erweiterte Problem für Ihre Anforderungen, wo, dass Sie nicht immer ' '
gespalten, nur für das ') OR ('
List<string> output = new List<string>();
string input = "(\"always use\" OR \"bar\") OR (\"Hello\" AND \"market cost\")";
string[] firstSplit = input.Split('"');
for (int i = 0; i < firstSplit.Length; i++)
{
firstSplit[i] = firstSplit[i].Trim();
// in case of the ') OR (' which has be splitted too
if (firstSplit[i].Contains(")") && firstSplit[i].Contains("("))
{
string[] secondSplit = firstSplit[i].Split(' ');
for (int j = 0; j < secondSplit.Length; j++)
{
output.Add(secondSplit[j]);
}
}
else
{
output.Add(firstSplit[i]);
}
}
Ergebnis:
(
always use
OR
bar
)
OR
(
Hello
AND
market cost
)
Vielen Dank, wirklich zu schätzen :) – roj123
hey, ich lief den Code und die "immer verwenden" Get Split auch :( – roj123
@ roj123 Ich machte Änderungen vor 39 Minuten, aber mit diesem Code oben sollte es funktionieren Ich kopiere und klebte es von hier, um es jetzt zu überprüfen und es funktionierte –