我是一名程序员,正在编写一个程序,需要从文本文件中的带引号字符串中删除分隔符字符。
例如:
"Hello, my name is world"
必须是:
"Hello my name is world"
这听起来一开始很容易(我也这么想),但你需要检测引用的开始和结束,然后搜索该特定字符串以查找分隔符。如何做到?
我尝试过一些正则表达式,但只是让自己感到困惑!
有任何想法吗?甚至只是为了开始推动球 rolling,我完全被卡住了。
我是一名程序员,正在编写一个程序,需要从文本文件中的带引号字符串中删除分隔符字符。
例如:
"Hello, my name is world"
必须是:
"Hello my name is world"
string pattern = "\"([^\"]+)\"";
value = Regex.Match(textToSearch, pattern).Value;
string[] removalCharacters = {",",";"}; //or any other characters
foreach (string character in removalCharacters)
{
value = value.Replace(character, "");
}
string pattern = "\"([^\"]+)\"";
它将从开放引号开始并在找到另一个引号时停止。 - Mark Avenius为什么不尝试使用 Linq 来完成呢?
var x = @" this is a great whatever ""Hello, my name is world"" and all that";
var result = string.Join(@"""", x.Split('"').
Select((val, index) => index%2 == 1 ?
val.Replace(",", "") : val).ToArray());
"\"(?=[^\"]+,)[^\"]+\""
。\"
匹配开头的双引号。前瞻(?=[^\"]+,)
会尝试匹配引号内的逗号。接下来,只要不是双引号就匹配字符串的其余部分[^\"]+
,然后匹配结束的双引号\"
。Regex.Replace
可以紧凑地修改结果并删除不需要的逗号。string input = "\"Hello, my name, is world\"";
string pattern = "\"(?=[^\"]+,)[^\"]+\"";
string result = Regex.Replace(input, pattern, m => m.Value.Replace(",", ""));
Console.WriteLine(result);
所以我猜你有一些带有很多引号的长文本?我会创建一个方法,类似于以下操作:
编辑
我刚刚想到了一个更好的方法。这个怎么样:
string mycompletestring = "This is a string\"containing, a quote\"and some more text";
string[] splitstring = mycompletestring.Split('"');
for (int i = 1; i < splitstring.Length; i += 2) {
splitstring[i] = splitstring[i].Replace(",", "");
}
StringBuilder builder = new StringBuilder();
foreach (string s in splitstring) {
builder.Append(s + '"');
}
mycompletestring = builder.ToString().Substring(0, builder.ToString().Length - 1);
我认为应该有更好的方法将字符串组合成一个带有“”的字符串,但我不知道更好的方法,所以请随意在这里建议一个好的方法 :)
protected virtual string[] delimitCVSBuffer(string inputBuffer) {
List<string> output = new List<string>();
bool insideQuotes = false;
StringBuilder fieldBuffer = new StringBuilder();
foreach (char c in inputBuffer) {
if (c == FieldDelimiter && !insideQuotes) {
output.Add(fieldBuffer.Remove(0, 1).Remove(fieldBuffer.Length - 1, 1).ToString().Trim());
fieldBuffer.Clear();
continue;
} else if (c == '\"')
insideQuotes = !insideQuotes;
fieldBuffer.Append(c);
}
output.Add(fieldBuffer.Remove(0, 1).Remove(fieldBuffer.Length - 1, 1).ToString().Trim());
return output.ToArray();
}
好的,这有点古怪,但它有效。
首先,您根据"
字符将字符串分成几个部分:
string msg = "this string should have a comma here,\"but, there should be no comma in this bit\", and there should be a comma back at that and";
var parts = msg.Split('"');
然后您需要在"
字符上将字符串重新连接在一起,在删除每个其他部分中的逗号之后:
string result = string.Join("\"", RemoveCommaFromEveryOther(parts));
删除函数的代码如下:
IEnumerable<string> RemoveCommaFromEveryOther(IEnumerable<string> parts)
{
using (var partenum = parts.GetEnumerator())
{
bool replace = false;
while (partenum.MoveNext())
{
if(replace)
{
yield return partenum.Current.Replace(",","");
replace = false;
}
else
{
yield return partenum.Current;
replace = true;
}
}
}
}
这需要你在代码中包含一个 System.Collections.Generic
的 using 指令。
有很多方法可以做到这一点:
查看函数string.Split()
和string.IndexOfAny()
您可以使用string.Split(new char[] {',',' '}, StringSplitOption.RemoveEmptyEntries)
将短语分割成单词,然后使用StringBuilder
类将单词组合在一起。
使用string.Replace("[char to remove goes here]"',"")
多次调用每个要删除的字符也可以实现。
编辑:
调用string.Split(new char[] {'\"'}, StringSplitOption.RemoveEmptyEntries)
以获取在引号(")之间的字符串数组,然后对每个字符串调用Replace
,最后使用StringBuilder
将字符串组合在一起。
thestring.replace("\", "")
是一样的,你需要在每个其他字符串中替换逗号,并且 StringSplitOption.RemoveEmptyEntries
意味着如果你的字符串以引号开头并且丢失任何空引号块,则得到错误的字符串顺序。-1 - Matt Ellen