我有一个字符串,如下所示,我想用函数的输出替换FieldNN实例。
到目前为止,我已经能够用函数的输出替换NN实例。但我不知道如何用相同的正则表达式删除静态“字段”部分。
输入字符串:
(Field30="2010002257") and Field1="yuan" not Field28="AAA"所需产出:
(IncidentId="2010002257") and Author="yuan" not Recipient="AAA"这是我到目前为止掌握的代码:
public string translateSearchTerm(string searchTerm) {
string result = "";
result = Regex.Replace(searchTerm.ToLower(), @"(?<=field).*?(?=\=)", delegate(Match Match) {
string fieldId = Match.ToString();
return String.Format("_{0}", getFieldName(Convert.ToInt64(fieldId)));
});
log.Info(String.Format("result={0}", result));
return result;
}这意味着:
(field_IncidentId="2010002257") and field_Author="yuan" not field_Recipient="aaa"我想解决的问题是:
我真的只需要解决第一个问题,其他三个将是一个额外的,但我可能会修复这些一旦我发现了正确的模式,空格和引号。
更新
我认为下面的模式解决了问题2和问题4。
result = Regex.Replace(searchTerm, @"(?<=\b(?i:field)).*?(?=\s*\=)", delegate(Match Match) 发布于 2014-01-28 20:46:40
要解决第一个问题,请使用组而不是积极的查找:
public string translateSearchTerm(string searchTerm) {
string result = "";
result = Regex.Replace(searchTerm.ToLower(), @"field(.*?)(?=\=)", delegate(Match Match) {
string fieldId = Match.Groups[1].Value;
return getFieldName(Convert.ToInt64(fieldId));
});
log.Info(String.Format("result={0}", result));
return result;
}在这种情况下,“字段”前缀将包括在每个匹配中,并将被替换。
https://stackoverflow.com/questions/21415970
复制相似问题