Full path with double backslash (C#) - 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";

Related

RegEx to replace string in C#

I'm really not great with RegEx in C#, never really used them but I have a long string that contains a lot of html that may contain numerous text parts like
src="Folder/Uploads/fd123051-532d-4804-a0fb-fd4ce6b70f7c/cd212dd7-7600-4b3f-a7d9-9a85c85a50ca.png"
or
src="Uploads/fd123051-532d-4804-a0fb-fd4ce6b70f7c/cd212dd7-7600-4b3f-a7d9-9a85c85a50ca.png"
I want to apply a reg ex over the string if it can be done in C# so it replaces the folder path so it will change any and all to be src = filename.extension
ie.
src="Uploads/fd123051-532d-4804-a0fb-fd4ce6b70f7c/cd212dd7-7600-4b3f-a7d9-9a85c85a50ca.png"
becomes
src="cd212dd7-7600-4b3f-a7d9-9a85c85a50ca.png"
Can anyone please help?
RegEx for your replace:
src="Uploads/fd123051-532d-4804-a0fb-fd4ce6b70f7c/cd212dd7-7600-4b3f-a7d9-9a85c85a50ca.png"
Will be:
F: src="(.+?)//(.+?)//(.+?).png" [You can check "Dot Matches All"]
R: src="$1/$2/$3.png" Or you can use instead of $1 , /1 /2 /3 etc.
You can use:
src = Path.GetFileName(src);
You need substring function that will take only the part which you want from string Please go here.
Get file name from path

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

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.

In C# a method for turning something like C:\mydir\another\again\..\..\ into C:\mydir

Is there a C# function to translate ..\s in a path so you can get the final path?
Path.GetFullPath should do it.
System.IO.Path.GetFullPath() will do what you want.
So you just wanna get the first folder and drop the rest? What about using a regex or string.substring?
As for the regex this one would get it:
\w:\\w+
and for the string substring
somthing like
newPath = path.Substring(4,path.IndexOf("\")-4);

Best way to escape javascript string? (json?)

Using C# .net I am parsing some data with some partial html/javascript inside it (i dont know who made that decision) and i need to pull a link. The link looks like this
http:\/\/fc0.site.net\/fs50\/i\/2009\/name.jpg
It came from this which i assume is javascript and looks like json
"name":{"id":"589","src":"http:\/\/fc0.site.net\/fs50\/i\/2009\/name.jpg"}
But anyways how should i escape the first link so i get
http://fc0.site.net/fs50/i/2009/name.jpg
In this case i could just replace '\' with '' since links dont contain \ nor " so i could do that but i am a fan of knowing the right solution and doing things properly. So how might i escape this. After looking at that link for a minute i thought is that valid? does java script or json escape / with \? It doesnt seem like it should?
In your case:
"name":{"id":"589","src":"http://fc0.site.net/fs50/i/2009/name.jpg"}
"/" is a valid escape sequence. However, it is not required that / be escaped. You may escape it if you need to. The reason JSON explicitly allows escaping of slash is because HTML does not allow a string in a to contain "...
Update:
Check out this post
Odd, it doesn’t look like any JavaScript/JSON escaping you’d expect. You can have forward slashes in JavaScript strings just fine.
Why dont you try a regex on the escaped slashes to replace them in the C# code...
String url = #"http:\/\/fc0.site.net\/fs50\/i\/2009\/name.jpg";
String pattern = #"\\/";
String cleanUrl = Regex.Replace(url, pattern, "/");
Hope it helps!
Actually you want to unescape the string. Answered in this question.

Categories

Resources