例如,我有一个string
"24.09.2019","545","878","5"
应处理为
"{1}","{2}","{3}","{4}"
现在我正在尝试使用正则表达式:
string replacementString="{NG}";
Regex regex = new Regex("\\\"[0-9\.]+\\\"");
MatchCollection matches = regex.Matches(originalString);
List<string> replacements = new List<string>();
for (int x = 0; x < matches.Count; x++)
{
string replacement = String.Copy(replacementString);
replacement = replacement.Replace("NG", (x + 1).ToString());
replacements.Add(replacement);
Match match = matches[x];
}
replacements.Reverse();
int cnt = 0;
foreach (var match in matches.Cast<Match>().Reverse())
{
originalStringTmp = originalStringTmp.Replace(
match.Index,
match.Length,
replacements[cnt]);
cnt++;
}
还有
public static string Replace(this string s, int index, int length, string replacement)
{
var builder = new StringBuilder();
builder.Append(s.Substring(0, index));
builder.Append(replacement);
builder.Append(s.Substring(index + length));
return builder.ToString();
}
但是在这种情况下,结果是
{1},{2},{3},{4}
我应该使用什么正则表达式代替
\"[0-9\.]+\"
获得结果
"{1}","{2}","{3}","{4}"
使用C#正则表达式?
答案 0 :(得分:1)
让我们尝试Regex.Replace
来替换字符串中所有的引号(我假设引号本身是转义的:"abc""def" -> abc"def
)
string source = "\"24.09.2019\",\"545\",\"878\",\"5\"";
int index = 0;
string result = Regex.Replace(source, "\"([^\"]|\"\")*\"", m => $"\"{{{++index}}}\"");
演示:
Func<string, string> convert = (source => {
int index = 0;
return Regex.Replace(source, "\"([^\"]|\"\")*\"", m => $"\"{{{++index}}}\"");
});
String[] tests = new string[] {
"abc",
"\"abc\", \"def\"\"fg\"",
"\"\"",
"\"24.09.2019\",\"545\",\"878\",\"5\"",
"name is \"my name\"; value is \"78\"\"\"\"\"",
"empty: \"\" and not empty: \"\"\"\""
};
string demo = string.Join(Environment.NewLine, tests
.Select(test => $"{test,-50} -> {convert(test)}"));
Console.Write(demo);
结果:
abc -> abc
"abc", "def""fg" -> "{1}", "{2}"
"" -> "{1}"
"24.09.2019","545","878","5" -> "{1}","{2}","{3}","{4}"
name is "my name"; value is "78""""" -> name is "{1}"; value is "{2}"
empty: "" and not empty: """" -> empty: "{1}" and not empty: "{2}"
编辑:您可以轻松地详细说明替换内容,例如如果您只想替换整数
Func<string, string> convert = (source => {
int index = 0;
// we have match "m" with index "index"
// out task is to provide a string which will be put instead of match
return Regex.Replace(
source,
"\"([^\"]|\"\")*\"",
m => int.TryParse(m.Value.Trim('"'), out int _drop)
? $"\"{{{++index}}}\"") // if match is a valid integer, replace it
: m.Value); // if not, keep intact
});
一般情况下
Func<string, string> convert = (source => {
int index = 0;
// we have match "m" with index "index"
// out task is to provide a string which will be put instead of match
return Regex.Replace(
source,
"\"([^\"]|\"\")*\"",
m => {
// now we have a match "m", with its value "m.Value"
// its index "index"
// and we have to return a string which will be put instead of match
// if you want unquoted value, i.e. abc"def instead of "abc""def"
// string unquoted = Regex.Replace(
// m.Value, "\"+", match => new string('"', match.Value.Length / 2));
return //TODO: put the relevant code here
}
});
答案 1 :(得分:0)
string originalString = "\"24.09.2019\",\"545\",\"878\",\"5\"";
var regex = new Regex("\"[0-9\\.]+\"");
var matches = regex.Matches(originalString);
string result = string.Join(',', Enumerable.Range(1, matches.Count).Select(n => $"\"{{{n}}}\""));
输入:
“ 24.09.2019”,“ 545”,“ 878”,“ 5”
结果:
“ {1}”,“ {2}”,“ {3}”,“ {4}”