字符串操作:如何用特定模式替换字符串 [英] String manipulation: How to replace a string with a specific pattern

查看:29
本文介绍了字符串操作:如何用特定模式替换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个与基于特定模式的字符串操作相关的问题.我正在尝试使用 C# 用预定义的模式替换特定模式

I've a question here related to string manipulation based on a specific pattern. I am trying to replace a specific pattern with a pre-defined pattern using C#

例如:

场景#1

Input: substringof('xxxx', [Property2])
Output: [Property2].Contains('xxxx')

该字符串可以在 linq 的 Where 子句中使用的位置.

where this string could be used within linq's Where clause.

我的溶胶:

var key= myString.Substring(myString.Split(',')[0].Length + 1, myString.Length - myString.Split(',')[0].Length - 2);
var value = myString.Replace("," + key, "").Replace([Key from Dictionary], [Value from Dictionary]);

 

Expected string: key + '.' + value.Replace("('", "(\"").Replace("')", "\")");

但这仅适用于上述情况.我想将其概括为以下所有场景.

But this works only for the above scenario. I would like to generalize it for all teh below scenario's.

场景:

Input: [Property1] == 1234 and substringof('xxxx', [Property2]) and substringof('xxxx', [Property3])
Output: [Property1] == 1234 and [Property2].Contains('xxxx') and [Property3].Contains('xxxx')

Input: substringof('xxxx', [Property2]) and [Property1] == 1234 and substringof('xxxx', [Property3])
Output: [Property2].Contains('xxxx') and [Property1] == 1234  and [Property3].Contains('xxxx')

任何帮助将不胜感激.非常感谢提前!

Any help would be appreciated. Thanks so much in advance!!

最终解决方案:

var replaceRegex = new Regex("substringof\\(\\s*'(?<text>[^']*)'\\s*,\\s*(?<pname>[\\w\\[\\]]+)\\s*\\)");
input = replaceRegex.Replace(input, "${pname}.Contains(\"${text}\")");

推荐答案

以下是一些似乎有效的示例代码:

Here's some sample code that seems to work:

System.Text.RegularExpressions.Regex replaceRegex = new System.Text.RegularExpressions.Regex("substringof\\(\\s*'(?<text>[^']*)'\\s*,\\s*(?<pname>[\\w\\[\\]]+)\\s*\\)");

string input1 = "[Property1] == 1234 and substringof('xxxx', [Property2]) and substringof('xx xx', [Property3])";
string input2 = "substringof('xxxx', [Property2]) and [Property1] == 1234 and substringof('xxxx', [Property3])";
string input3 = "(Id > 0 and substringof('2', Name))";

string output1 = replaceRegex.Replace(input1, "${pname}.Contains('${text}')");
string output2 = replaceRegex.Replace(input2, "${pname}.Contains('${text}')");
string output3 = replaceRegex.Replace(input3, "${pname}.Contains('${text}')");

请注意,我为一些内部空白添加了容差,并对要匹配的文本进行了假设.引号和/或属性标识符中可以包含哪些类型的字符?这可能需要进行调整以适应这些要求.

Note that I added tolerance for some internal whitespace and made assumptions about the text to be matched. What kinds of characters can be included inside the quotes and/or property identifiers? This may need to be tweaked to fit those requirements.

我做了一些主动调整.将 \w* 更改为 [^']* 意味着它将匹配空格或符号或其他任何内容,直到到达结束引号,然后停止匹配.这更符合标准编程语言.属性名称的限制更加严格:\w 将匹配字母、数字和下划线字符.这些都不能替代适当的解析器/词法分析器来捕获错误并明确识别它们,但它可能会在紧要关头.

I did some proactive tweaking. Changing \w* to [^']* means it will match spaces or symbols or whatever until it reaches a closing quote, then stop matching. This is a little more in line with standard programming languages. The property name is kept more restrictive: \w will match letters, numbers, and underscore characters. None of this is a substitute for a proper parser/lexer to catch errors and identify them explicitly, but it might do in a pinch.

编辑 2: 已更新以删除对括号的要求.请注意,这是非常宽容的:该模式将匹配诸如 substringof('xxxx', [[Property3]morestuffhere[) 之类的奇数字符串,因为它只是假设 [ 和 ] 是您的标识符中的有效字符.无论是否有括号,它都不允许使用符号或空格.请注意,替换字符串也已更改.如果您不删除方括号(正如我在示例中所做的那样),您可能会以双括号结束.

EDIT 2: Updated to remove the requirement for brackets. Note that this is very tolerant: the pattern will match odd strings like substringof('xxxx', [[Property3]morestuffhere[) because it just assumes that [ and ] are valid characters in your identifier. It will not allow symbols or spaces, regardless of whether there are brackets or not. Note that the replacement string has also changed. If you don't remove the square brackets (as I have done in the sample), you could end up with double brackets.

这篇关于字符串操作:如何用特定模式替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆