Here is my function, I'm trying to replace a string in a file, but c# tells me my regex is malformed. Any ideas?
public void function(string fileName, string path) {
string pathToAmmend = #"$SERVERROOT\pathpath";
string newPath = #"$SERVERROOT\" + path;
File.WriteAllText(fileName, Regex.Replace(File.ReadAllText(fileName), pathToAmmend, newPath));
....
}
It works if i change the strings to:
string pathToAmmend = #"$SERVERROOT\\pathpath";
string newPath = #"$SERVERROOT\\" + path;
But then I have two slashes and I only want one slash.
It sounds like you don't actually need a regular expression at all. It sounds like you quite possibly just want string.Replace:
// Split into three statements for clarity.
string input = File.ReadAllText(fileName);
string output = input.Replace(pathToAmend, newPath);
File.WriteAllText(output);
Only use regular expressions when you're genuinely trying to match patterns.
A \ is a special escaping character in regular expressions. You have to escape it so that it will be interpreted as a literal \ and not an escape sequence. $ is also a special character (an end anchor), so you'll want to escape that as well.
string pathToAmmend = #"\$SERVERROOT\\pathpath";
Using # to create a verbatim string only means you don't have to escape the \ for the sake of the C# compiler. You still have escape the \ for in a regular expression pattern. Without the verbatim string this would be:
string pathToAmmend = "\\$SERVERROOT\\\\pathpath";
Of course, as Jon Skeet points out, for something this simple, regular expressions aren't really the best way to go here.
Related
I am really confused here. I have written a snippet of code in C# that is passed a possible file pathway. If it contains a character specified in a regex string, it should return false. However, the regex function Match refuses to find anything matching (I even set it to a singular character I knew was in the string), resulting in severe irritation from me.
The code is:
static bool letterTest(string pathway)
{
bool validPath = false;
char[] c = Path.GetInvalidPathChars();
string test = new string(c);
string regex = "["+test+"]";
string spTest = "^[~#%&*\\{}+<>/\"|]";
Match match = Regex.Match(pathway, spTest);
if (!match.Success)
{
validPath = true;
}
return validPath;
}
The string I pass to it is: #"C:/testing/invalid#symbol"
What am I doing wrong/misunderstanding with the regex, or is it something other than the regex that I have messed up?
Remove the initial caret from your regex:
[~#%&*\\{}+<>/\"|]
You are requiring that the path begin with one of those characters. By removing that constraint, it will search the whole string for any of those characters.
But why not use the framework to do the work for you?
Check this out: Check if a string is a valid Windows directory (folder) path
Instead of a regular expression you can just do the following.
static bool letterTest(string pathway)
{
char[] badChars = Path.GetInvalidPathChars();
return pathway.All(c => !badChars.Contains(c));
// or
// return !pathway.Any(c => badChars.Contains(c));
// or
// return badChars.All(bc => !pathway.Contains(bc));
// or
// return !badChars.Any(bc => pathway.Contains(bc));
}
Someone has already pointed out the caret that was anchoring your match to the first character. But there's another error you may not be aware of yet. This one has to do with your use of string literals. What you have now is a traditional, C-style string literal:
"[~#%&*\\{}+<>/\"|]"
...which becomes this regex:
[~#%&*\{}+<>/"|]
The double backslash has become a single backslash, which is treated as an escape for the following brace (\{). The brace doesn't need escaping inside a character class, but it's not considered a syntax error.
However, the regex will not detect a backslash as you intended. To do that, you need two backslashes in the regex, so there should be four backslashes in the string literal:
"[~#%&*\\\\{}+<>/\"|]"
Alternatively, you can use a C# verbatim string literal. Backslashes have no special meaning in a verbatim string. The only thing that needs special handling is the quotation mark, which you escape by adding another quotation mark:
#"[~#%&*\\{}+<>/""|]"
you have to escape the / literal
"^[~#%&*\\{}+<>\/\"|]"
Caret stands for negation of the character group. Removing it from spTest solves this issue.
string spTest = "[~#%&*\\{}+<>/\"|]";
I am trying to extract all of the text (shown as xxxx) in the follow pattern:
Session["xxxx"]
using c#
This may be Request.Querystring["xxxx"] so I am trying to build the expression dynamically. When I do so, I get all sorts of problems about unescaped charecters or no matches :(
an example might be:
string patternstart = "Session[";
string patternend = "]";
string regexexpr = #"\\" + patternstart + #"(.*?)\\" + patternend ;
string sText = "Text to be searched containing Session[\"xxxx\"] the result would be xxxx";
MatchCollection matches = Regex.Matches(sText, #regexexpr);
Can anyone help with this as I am stumped (as I always seem to be with RegEx :) )
With some little modifications to your code.
string patternstart = Regex.Escape("Session[");
string patternend = Regex.Escape("]");
string regexexpr = patternstart + #"(.*?)" + patternend;
The pattern you construct in your example looks something like this:
\\Session[(.*?)\\]
There are a couple of problems with this. First it assumes the string starts with a literal backslash, second, it wraps the entire (.*?) in a character class, that means it will match any single open parenthesis, period, asterisk, question mark, close parenthesis or backslash. You'd need to escape the the brackets in your pattern, if you want to match a literal [.
You could use a pattern like this:
Session\[(.*?)]
For example:
string regexexpr = #"Session\[(.*?)]";
string sText = "Text to be searched containing Session[\"xxxx\"] the result would be xxxx";
MatchCollection matches = Regex.Matches(sText, #regexexpr);
Console.WriteLine(matches[0].Groups[1].Value); // "xxxx"
The characters [ and ] have a special meaning with regular expressions - they define a group where one of the contained characters must match. To work around this, simply 'escape' them with a leading \ character:
string patternstart = "Session\[";
string patternend = "\]";
An example "final string" could then be:
Session\["(.*)"\]
However, you could easily write your RegEx to handle Session, Querystring, etc automatically if you require (without also matching every other array you throw at it), and avoid having to build up the string in the first place:
(Querystring|Session|Form)\["(.*)"\]
and then take the second match.
I need to replace double quotes with single so that something like this
\\\\servername\\dir1\\subdir1\\
becomes
\\servername\dir1\subdir1\
I tried this
string dir = "\\\\servername\\dir1\\subdir1\\";
string s = dir.Replace(#"\\", #"\");
The result I get is
\\servername\\dir1\\subdir1\\
Any ideas?
You don't need to replace anything here. The backslashes are escaped, that's why they are doubled.
Just like \t represents a tabulator, \\ represents a single \. You can see the full list of Escape Sequences on MSDN.
string dir = "\\\\servername\\dir1\\subdir1\\";
Console.WriteLine(dir);
This will output \\servername\dir1\subdir1\.
BTW: You can use the verbatim string to make it more readable:
string dir = #"\\servername\dir1\subdir1\";
There is no problem with the code for replacing. The result that you get is:
\servername\dir1\subdir1\
When you are looking at the result in the debugger, it's shown as it would be written as a literal string, so a backslash characters is shown as two backslash characters.
The string that you create isn't what you think it is. This code:
string dir = "\\\\servername\\dir1\\subdir1\\";
produces a string containing:
\\servername\dir1\subdir1\
The replacement code does replace the \\ at the beginning of the string.
If you want to produce the string \\\\servername\\dir1\\subdir1\\, you use:
string dir = #"\\\\servername\\dir1\\subdir1\\";
or:
string dir = "\\\\\\\\servername\\\\dir1\\\\subdir1\\\\";
This string "\\\\servername\\dir1\\subdir1\\" is the same as #"\\servername\dir1\subdir1\". In order to escape backslashes you need either use # symbol before string, or use double backslash instead of one.
Why you need that? Because in C# backslash used for escape sequences.
This is probably a really simple question but I can't seem to get my head around it. I need to have a string that contains \" without it seeing it as an escape character. I tried using # but it won't work. The only other way I thought of doing this would be to use \u0022 but don't want to unless I can help it.
Desired string - string s = "\"\""; // Obviously this doesn't work!
Desired console output - \"\"
Hope this makes sense.
Thanks!
Try
string s = "\\\"\\\"";
You have to escape your backslashes too.
Mike
You can use the literal, but you need to double-up quotes.
string s = #"\""\""";
In verbatim string literals (#"...") a " in the string value is encoded as "", which happens to also be the only escape sequence in verbatim strings.
#"\""Happy coding!\""" // => \"Happy coding!\"
"\\\"Happy coding!\\\"" // => \"Happy coding!\"
Note that in the 2nd case (not a verbatim string literal), a \ is required before the \ and the " to escape them and prevent their normal meanings.
See the C# string reference for more details and examples.
I think you have to escape backslashes too... so something like "\\\"\\\"" should work, I believe.
Use this string:
string s = "\\\"\\\"";
Console.WriteLine( "\\\"\\\"" );
Just put a \ before each character that needs to be printed.
String s = #"\""\""";
DblQuote characters will escape a second dblquote character
Though for better readability I would go with:
const String DOUBLEQUOTE = """";
const String BACKSLASH = #"\";
String s = BACKSLASH + DOUBLEQUITE + BACKSLASH + DOUBLEQUOTE;
In a verbatim string (a string starting with #"") to escape double quotes you use double quotes, e.g. #"Please press ""Ok"".". If you want to do it with verbatim strings then you would do something like #"\""" (that's 3 double quotes on the end there).
You can do like this,
string s = "something'\\\'";
Use a single '' rather then "" in string to do the same.
I am using regular expression in code behind file and defining string as
string ValEmail = "\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*";
if (Regex.IsMatch(email, "\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"))
{ }
else
{ }
It gives me warning and does not compile. How can I define such string combination?.
In C# the backslash is a special character, if it is to represent a backslash we need to inform the compiler as such.
This can be achieved by escaping it with a backslash:
string ValEmail = "\\w+([-+.']\\w+)*#\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
Or using an # prefix when constructing the string:
string ValEmail = #"\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*";
The backslash is the escape char in c# strings. Technically you have to escape the backslash with another blackslash ("\\") or just add an # before your string:
string ValEmail = #"\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*";
Use #"\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" so the backslashes will get escaped