Converting VB.net replace to C# - c#

I want convert this VB.net snippet to C#
sURL = Replace(sURL,"%F9","%C3%B9",,,CompareMethod.Text)
Which one of these is better?
sURL = Strings.Replace(sURL,"%F3","%C3%B3", 1, -1, CompareMethod.Text);
sURL = Regex.Replace(sURL,"%FA","%C3%BA",CompareMethod.Text);

Regex replace is used for Regular expressions. Here you haven't regular expression, so it's better to use usual replace:
sURL = sURL.Replace("%F3","%C3%B3");

I simple String.Replace will be more efficient than Regex.Replace when you are doing a straightforward text-replace. If you don't need any of the features of Regex, it's best not to use it.

Related

Replace characters using Regex in c# [duplicate]

I like to know how to replace a regex-match of unknown amount of equal-signs, thats not less than 2... to the same amount of underscores
So far I got this:
text = Regex.Replace(text, "(={2,})", "");
What should I use as the 3rd parameter ?
EDIT: Prefferably a regex solution thats compatible in all languages
You can use Regex.Replace(String, MatchEvaluator) instead and analyze math:
string result = new Regex("(={2,})")
.Replace(text, match => new string('_', match.ToString().Length));
A much less clear answer (in term of code clarity):
text = Regex.Replace(text, "=(?==)|(?<==)=", "_");
If there are more than 2 = in a row, then at every =, we will find a = ahead or behind.
This only works if the language supports look-behind, which includes C#, Java, Python, PCRE... and excludes JavaScript.
However, since you can pass a function to String.replace function in JavaScript, you can write code similar to Alexei Levenkov's answer. Actually, Alexei Levenkov's answer works in many languages (well, except Java).

C# Regular Expression Reversing Match

I am looking to convert a part of a string which is substringof('has',verb) into contains(verb,'has')
As you can see, what is changing is just substring to contains and the two parameters passed to the function reversed.
I am looking for a generic solution, by using regex. Preferably using tags. i.e once i get two matches, i need to be able to reverse the matches by using $2$1 (This is how i remember doing this in perl)
You can use this regular expression code:
var re = new Regex(#"substringof\('([^']+)',([^)]+)\)");
string output = re.Replace(input, #"contains($2, '$1')");
.NET Fiddle example
You can use a regex like this:
.*?\((.*?),(.*?)\)
Working demo
Then you can use a string replacement like this:
contains(\2,\1) or
contains($2,$1)
Btw, if you just want to change the substringof, then you can use:
substringof\((.*?),(.*?)\)

ereg_replace equivalent in C#

I need to know how can we use $no = ereg_replace(" .*", "", $command[$i]); equivalent in c#, I am very newbie in pattern matching, can anyone please let me know the examples of pattern matching in c#?
Maybe you can use Regex.
In this example, we want to delete all of HTML tags that we have in string.
string newDescription = Regex.Replace("hello how are <b>you</b>", "<[^>]*>", string.Empty);
newDescription now have "hello how are you"

Using replace function with regex in C Sharp

Need some help on a problem please.
In fact I got a base64 string named "image" like that :
data:image/pjpeg;base64,iVBORw0KGgoAAAANSUhE...
I need to replace the part "data:image/pjpeg;base64," by "".
I try this way :
imageSrc = image.Replace("data:image/(png|jpg|gif|jpeg|pjpeg|x-png);base64,", "");
But it doesn't work.
Is somebody has an idea on that.
Thanks a lot
You should use the static Replace method on the Regex class.
imageSrc = Regex.Replace(image, "data:image/(png|jpg|gif|jpeg|pjpeg|x-png);base64,", "");
Well, for starters your code is doing String.Replace instead of Regex.Replace.
imageSrc = Regex.Replace(image, "data:image/(png|jpg|gif|jpeg|pjpeg|x-png);base64,", "");
But Regex is a rather heavy for this use case, why not just take everything after the comma?
imageSrc = image.SubString(image.IndexOf(",") + 1);
You are just using String.Replace, but you should use Regex.Replace for regular expressions.
But why not just use Substring?
imageSrc = image.Substring(image.IndexOf(',') + 1)
Since you know that your string is always starting with data:image/..., you don't need regular expressions at all.
Keep it simple and just take the substring after the first ,.
String.Replace() has no overload with regexp. Use Regex.Replace() instead.
There is a mistake in your regex, you must specify ?: for images alternatives and use Regex object, so :
Regex.Replace("data:image/(?:png|jpg|gif|jpeg|pjpeg|x-png);base64,", "");
it should work

How to escape a C# regular expression in the correct way?

This is my regex in plain form:
<tr>[\s]+?<td class="filePathActiv"[\w\W]+?<div class="Overflow">[\s]*?(?<name>[\w\W]+?)&nbsp[\w\W]+?\(wygasa (?<wygasa>[\d\s\-\:\s]+)\)[\w\W]+?class="fileDown"[\w\W]+?<a href="(?<address>[\w\W]+?)">[\w\W]+?<td width="[\d]+?">(?<sizeMB>[\d\.]+?) MB</td>[\w\W]+?<input [\w\W]+? name="(?<path>[\w\W]+?)" [\w\W]+? value="(?<value>[\w\W]+?)"[\w\W]+?/>
How should I escape this regex? I tried like that but it's not working:
new Regex("<tr>[\\s]+?<td class=\"filePathActiv\"[\\w\\W]+?<div class=\"Overflow\">[\\s]*?(?<name>[\\w\\W]+?)&nbsp[\\w\\W]+?\\(wygasa (?<date>[\\d\\s\\-\\:\\s]+)\\)[\\w\\W]+?class=\"fileDown\"[\\w\\W]+?<a href=\"(?<address>[\\w\\W]+?)\">[\\w\\W]+?<td width=\"[\\d]+?\">(?<sizeMB>[\\d\\.]+?) MB</td>[\\w\\W]+?<input [\\w\\W]+? name=\"(?<path>[\\w\\W]+?)\" [\\w\\W]+? value=\"(?<value>[\\w\\W]+?)\"[\\w\\W]+?/>", RegexOptions.IgnoreCase);
You should put it in a #"..." string.
Then, all you need to do is replace every " with "".
I think you should use Regex.Escape() method.

Categories

Resources