String processing: how to replace a string with a specific pattern

I have a question related to string manipulation based on a specific pattern. I am trying to replace a specific template with a predefined template using C #

For instance,

Scenario number 1

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

where this line can be used in the linq Where expression.

My angry:

 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 only works for the above scenario. I would like to generalize it to all the below scenarios.

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 greatly appreciated. Thank you very much in advance!

Final decision:

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

Here is an example of code that works:

 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 to some of the internal spaces and made assumptions about the consensus text. What types of characters can be included in quotation marks and / or property identifiers? It may be necessary to meet these requirements.

EDIT: I made some presets. Changing \ w * to [^ '] * means that it will match spaces or characters or the like until it reaches the final quote, and then stop matching. This is a little more consistent with standard programming languages. The property name is stored more strictly: \ w will match letters, numbers, and underscores. None of this can replace the correct parser / lexer to catch errors and identify them explicitly, but this can be done as a last resort.

EDIT 2: Updated to remove the requirement for parentheses. 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 does not allow characters or spaces, regardless of whether there are brackets or not. Note that the replacement string has also changed. If you do not remove the square brackets (as I did in the sample), you can get double brackets.

+3
source

It is difficult to say on your question what is changing and what remains. Assuming that

  • substringof modifies (and can be any alphanumeric identifier)
  • 'xxxx' changes, but is always enclosed in single quotes,
  • [Property2] changes (and does not have to be in square brackets)

here is a sample code that will help you:

 using System; using System.Text.RegularExpressions; public class Test { public static void Main() { Console.WriteLine(Convert("substringof('xxxx', [Property2])")); Console.WriteLine(Convert("[Property1] == 1234 and substringof('xxxx', [Property2]) and substringof('xxxx', [Property3])")); Console.WriteLine(Convert("substringof('xxxx', [Property2]) and [Property1] == 1234 and substringof('xxxx', [Property3])")); } public static string Convert(string str) { Regex r = new Regex("(\\w+)\\(\\s*('[^']*')\\s*,\\s*([^)]+?)\\s*\\)"); return r.Replace(str, new MatchEvaluator(MatchEvaluatorDelegate)); } public static string MatchEvaluatorDelegate(Match m) { string answer = ""; answer += m.Groups[3].Value + "."; answer += m.Groups[1].Value.Replace("substringof", "Contains"); answer += "(" + m.Groups[2].Value + ")"; return answer; } } 

Here is an ideon demonstrating this code. Output:

 [Property2].Contains('xxxx') [Property1] == 1234 and [Property2].Contains('xxxx') and [Property3].Contains('xxxx') [Property2].Contains('xxxx') and [Property1] == 1234 and [Property3].Contains('xxxx') 

Of course, you need to go ahead and change the hard-written replacement of substringof to Contains with what you do with your dictionary.

+1
source

Source: https://habr.com/ru/post/1479930/


All Articles