Find and replace custom values ​​in C # string

I am involved in developing an application that takes the contents of a PHP file and stores them in a variable.

The user enters what characters / values ​​need to be replaced in the downloaded content.

For instance:

FIND REPLACE echo Print <?php <? CURL (space) 

Then these templates can be saved to a text file for future use. When a user wants to remove content according to a template, he can do this by clicking a button.

So my question is:

Can this be easily archived using the basic string replacement method, or do I need to move on to a more complex regular expression?

And if I have to use regex, how do I create custom regex patterns based on user input?

I would really appreciate it if you can help me with this, thanks.

+4
source share
5 answers

This can be achieved with a simple Replace , a regular expression to handle such complex requirements will not only be messy, but it will also be incredibly difficult to maintain / update with additional elements.

How about saving your “Find” and “replace” templates in the Dictionary , and then loop through and replace? That way, if you add more elements, just add them to your Dictionary . Sort of:

 Dictionary<string, string> replacements = new Dictionary<string, string> { { "echo", "PRINT" }, { "<?php", "<?" }, { "CURL", "(space)" } } string yourString; //this is your starter string, populate it with whatever foreach (var item in replacements) { yourString = yourString.Replace(item.Key, item.Value); } 
+3
source

If you are just trying to replace the character set in a given string with another character set, then the string.Replace() method will suit you just fine.

The goal of any programmer is to write the simplest possible code for a given task. While regular expressions are well suited for contextual replacements, if all you need is a basic search and replace function, then they are definitely crowded and will only serve to introduce unnecessary complexity into your program.

+3
source

None of the answers that I have seen so far mention being careful to make sure that the iterative approach does not match FIND values ​​that were not in the original source (for example, your first loop can replace echo with Print , but then loop 2 can replace int (from a new Print , for example Int32 , leaving PrInt32 ).

You can be “safer” to use a very simple regular expression to make sure that you are only replacing the whole word, which will at least protect you from my example above.

For example, instead of searching for the user-specified string int , wrap it within the regular expression word boundaries and find \bint\b instead (of course, thorough testing will be needed to make sure that this works with any operators that may be in the target file or that it suitable for these types of search strings, etc.). You can then provide this as an option (similar to most text editing programs that provide the option "just match the whole word").

You also need to make sure that you never repeat the same search target with the same replacement - for example, make sure that <? won't become <?php , and then become <?<?php , etc. I don’t think that any of the answers given here will suffer from this problem.

+3
source

You can use the dictionary to more quickly repeat Replace calls:

 var replacements = new Dictionary<string, string>(); replacements.Add("echo", "print"); replacements.Add("<?php", "<?"); ... foreach(var pair in replacements) { myString = myString.Replace(pair.Key, pair.Value); } 

Or use the Linq Aggregate method.

 myString = replacements.Aggregate(myString, (s, p) => s.Replace(p.Key, p.Value)); 

This will work for simple strings, but the same general design can be used for regular expression patterns:

 var replacements = new Dictionary<string, string>(); ... foreach(var pair in replacement) { myString = new RegEx(pair.Key).Replace(myString, pair.Value); } 

And again with Linq:

 myString = replacements.Aggregate(myString, (s, p) => new RegEx(pair.Key).Replace(s, p.Value)); 
+1
source

Just using String.Replace is the easiest way, but it won't be the fastest. I would try it first, and then only if it doesn't switch fast enough to using regular expressions. You must be careful that the replacement text may match one of your search fragments.

To use regular expressions, you need to Regex.Escape each search string, and then combine all escaped patterns using '|' as a separator. Then you can make one Regex.Replace using MatchEvaluator to search for notes.

0
source

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


All Articles