Using replace function with regex in C Sharp - c#

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

Related

Converting VB.net replace to 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.

Correction in this simple regular expression

I am new to regular expressions and the one that i have written might be a very simple one but donot know where I am wrong.
#"^([a-zA-Z._]+)#([\d]+)"
This RE is for the following string:
somename#somenumber
Now i am trying to retrieve the somename and somenumber. This is what i did:
ac.name = m.Groups[0].Value;
ac.number = m.Groups[1].Value;
Here ac.name reads the complete string, and ac.number reads somenumber. Where am I wrong in ac.name?
i guess the regex is correct, the problem is, you get the ac.name not from group 1 but group(0), which is the whole string. try this:
ac.name = m.Groups[1].Value;
ac.number = m.Groups[2].Value;
This regex is correct. I think your mistake is in somewhere else. You seem to use C#. So, you should think about the regex usage in the language.
Looking to the code sample in MSDN, you need to use 1-based indexes while accessing Groups instead of zero-based (as also Kent suggested). So, use this:
String name = m.Groups[1].Value;
String number = m.Groups[2].Value;
use this regex (\w+)#(\d+([.,]\d+)?)
Groups[1] will be contain name
Groups[2] will be contain number
I think you should move the + into the capture group:
#"^([a-zA-Z._]+)#([\d]+)"
If this is C#, try without the ^
([a-zA-Z\._]+)#([\d]+)
I just tried it out and it groups properly
Update: escaped the .
If you want only one match (and hence the ^ in original expression), use .Match instead of .Matches method. See MSDN documentation on Regular Expression Classes.

C# Regex.replace Pattern needed

Usually I make my Regex patterns by myself, but I can't figure this one out:
I need a Regex.Replace that replaces "'Number'/'Number'" to "'Number'von'Number'".
Example: "2/5" schould become "2von5".
The problem is that I can't just replace "/" to "von" because there are other "/" that are needed.
You can replace (?<=\d)/(?=\d) with von, using lookaround.
Another option is to replace (\d)/(\d) with $1von$2 (though that would fail on 1/2/3).

Full path with double backslash (C#)

Is it possible to get a full path with double backslash by using Path.GetFullPath? Something like this:
C:\\Users\\Mammamia\\Videos\\Documents\\CFD\\geo_msh\\cubeOp.txt
instead of this:
C:\Users\Mammamia\Videos\Documents\CFD\geo_msh\cubeOp.txt
Or is there any other method?
Do you mean this?
Path.GetFullPath(path).Replace(#"\", #"\\");
C:\\Users\\Mammamia\\Videos\\Documents\\CFD\\geo_msh\\cubeOp.txt is not a valid path, so I'm not sure why you'd want it, but:
Path.GetFullPath(yourPath).Replace("\\", "\\\\");
You can just do this:
Path.GetFullPath(#"C:\\Users\\Mammamia\\Videos\\Documents\\CFD\\geo_msh\\cubeOp.txt")
But i'm not sure why, you want to escape the \ ?
If yes, you can do just this :
Path.GetFullPath(#"C:\Users\Mammamia\Videos\Documents\CFD\geo_msh\cubeOp.txt")
I would recommend doing a String.replace(). I recently had to do this in a project for myself. So if you do something similar to:
String input = Path.GetFullPath(x);
input = input.Replace("\\","\\\\");
I am fairly confident that is what you need :)
Documentation:
http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx
just apply an # sign before the string:
String k = #"C:\temp\myfile.txt";

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