This question already has answers here:
Regex escape with \ or \\?
(5 answers)
Closed 4 years ago.
I'm getting the not enough )'s error, but as best I can tell I've got all the )'s in.
line getting error:
string pathFriendlyDevId = Regex.Match(device, "VEN.*(?=\\)").ToString().Replace("&", "_");
device string getting parsed:
string device = "PCI\\VEN_144D&DEV_A804&SUBSYS_A801144D&REV_00\\4&10B60712&0&00EA";
the goal here is to get everything from VEN to REV_00.
Am I missing something obvious here?
As was pointed out above, using a verbatim string such as #"VEN.*(?=\\)" did the trick
Related
This question already has answers here:
Remove dots from the path in .NET
(10 answers)
Closed 1 year ago.
Is there a built in way of removing unnessacary statements like this \hello\.. from a path in C# or do I have to do this using regex replace?
Example:
C:\Users\me\myfolder\..\anotherFolder\image.png to C:\Users\me\anotherFolder\image.png
C:\Users\me\myfolder\..\..\you\f\image.png to C:\Users\you\f\image.png
my solution using regex would be removing this /\\([^\.\\]+)\\\.\./ using a loop regexr.com
You should just be able to write this:
string fixedPath = Path.GetFullPath(#"C:\Users\me\myfolder\..\anotherFolder\image.png"));
Result in fixedPath:
C:\Users\me\anotherFolder\image.png
This question already has answers here:
How to replace straight quotation mark (")
(3 answers)
Closed 6 years ago.
Some string variables have a " in the data which recks with my php code. I want to take it out in my C# code before the data gets passed to php. I just can't remember the way of doing it, but I tried with this :
line.Replace(#""", "");
This is not correct in Xamarin, what is the good syntax?
I guess this would work.
line.replace("\"","");
use \ to escape it.
line.Replace("\"", "");
This question already has answers here:
How to escape braces (curly brackets) in a format string in .NET
(11 answers)
Closed 7 years ago.
I am getting this error with the following line of code,
int numberStored = 9;
record.VALUE = string.Format("{\"FIELDS\":[{\"ELEMENT_ID\":\"275887826\",\"VALUE\":\"{0}\"}]}", numberStored.ToString(), 0);
This works fine if i substitute out {0} and place the 9 in directly, but obviously I don't want it hard coded like this. The previous answers I can see for this problem do not seem to be helping me.
You have to escape curly braces by doubling them:
int numberStored = 9;
record.VALUE = string.Format("{{\"FIELDS\":[{{\"ELEMENT_ID\":\"275887826\",\"VALUE\":\"{0}\"}}]}}", numberStored.ToString(), 0);
This question already has answers here:
Why does the Replace() string method not modify my string variable?
(4 answers)
Closed 9 years ago.
string mystring="the are boys";
string[] tags = {"the"};
string[] replace ={"they"}
mystring.Replace(tags[0],replace[0]) // is not working
mystring.Replace("the","they") // is working
I thought both are same but first statement is not working. The second one is.
Please help me to solve the problem.
I assume that you don't assign the return value of String.Replace to the variable. But since strings are immutable you have to do that:
mystring = mystring.Replace(tags[0],replace[0])
This question already has answers here:
Full path with double backslash (C#)
(5 answers)
Closed 9 years ago.
I'm doing a C# project for school.
I need to extract a .zip file but i have a problem.
I get the path where the file that is going to be extract is with FolderBrowserDialog and everything is ok, but the FolderBrowserDialog gives me something like "C:\Users\Zé Eduardo\Music" , but i need something like this "C:\\Users\\Zé Eduardo\\Music".
How can i transform "\" to "\\"?
well, this is the answer to your question but you are probably asking the wrong question,
var transformedString = badString.Replace(#"\", #"\\");
The # in the literal means, this is a verbatim string so normal escaping rules don't apply. Effectively, you don't need to escape the escape character.
Something simple would be to use string replace:
String original = #"c:\some\path";
String #fixed = original.Replace("\\", "\\\\"); //Note the double escaping!
//fixed contains "c:\\some\\path"