Replacing Paths in a File with Regex in C# - c#

So, I have this text file containing a few paths to files. Those paths need to be partially replaced. This needs to be case insensitive, so I tried doing this with Regex.
A part of the file looks like this:
PATH1 = d:\Software\system\SETUP\folder1
PATH2 = d:\Software\system\SETUP\folder2
PATH3 = d:\Software\system\SETUP
The first part: d:\Software\system needs to be replace with c:\Software\system
I tried this with the following code:
string text = File.ReadAllText(filePath);
string pattern = "d:\\Software\\system";
string replace = "C:\\Software\\system";
string newText = Regex.Replace(text, pattern, replace, RegexOptions.IgnoreCase);
File.WriteAllText(filePath, newText);
This however does not change anything in the file. I also used breakpoints to analize the value of newText after the Replace line, and it does not have anything to do with the writing of the file.
Any help is most appreciated!

Use a Regex.Escape method that will escape your literal pattern:
Here is a testing code:
var text = #"PATH1 = d:\Software\system\SETUP\folder1
PATH2 = d:\Software\system\SETUP\folder2
PATH3 = d:\Software\system\SETUP";
string pattern = "d:\\Software\\system";
string replace = "C:\\Software\\system";
string newText = Regex.Replace(text, Regex.Escape(pattern), replace.Replace("$", "$$"), RegexOptions.IgnoreCase);
// ^^^^^^^^^^^^^
Console.WriteLine(newText);
Another way is to use Microsoft.VisualBasic.Strings.Replace that you need to use with CompareMethod.Text:
Text
Optional. Results in string comparisons based on a case-insensitive text sort order determined by your system's locale.
This type of comparison is useful if your strings contain all text characters, and you want to compare them taking into account alphabetic equivalences such as case insensitivity and closely related letters. For example, you might want to consider A and a to be equal, and Ä and ä to come before B and b.
Code:
var newText = Microsoft.VisualBasic.Strings.Replace(text,
pattern,
replace,
Compare: Microsoft.VisualBasic.CompareMethod.Text);

In your pattern \S means Not white space and \s means white space. You need to make the backslashes literal by escaping them with their own backslashes:
So either:
string pattern = #"d:\\Software\\system";
Or:
string pattern = "d:\\\\Software\\\\system";

For file paths you can convert path, pattern and replace .ToUpper() and then do a normal string newPath = path.Replace(pattern, replace);
P.S. If you the resulting path string casing matters — the Regex.Escape solution is better.

Related

split the string containing backward slash and replace with forward slash

I have a string which contains backward slashes and I want to reply it with forward slashes
string filename = "te\test";
var x = filename.Split('\\');
Console.WriteLine(filename);
Console.ReadLine();
I have tried something like this but it is getting the same string "te\test" into x.
Is there any other way to do this?
You original string is not:
te\test
it's:
te{tab}est
\t is the tab character. So you can't split on the \ because you original string doesn't have a \
If you do something like this:
string filename = "te\\test";
var x = filename.Split('\\');
Console.WriteLine(string.Join("/",x));
You'll get the result you wanted.
But you really don't need to Split and Join when you can just Replace:
Console.WriteLine(filename.Replace('\\','/'));
Note: you can use # with your original string to make it a literal string (escapes are ignored) as #Joeb454 suggests (and that's usually what I'll do), but unfortunately the same trick doesn't apply to chars so you can't, for example, do #'\'.
Your initial string appears to be wrong, you're escaping the 't', giving you a tab character, it should be string filename = "te\\test";
You could also declare it as string filename = #"te\test"; - preceding the string with the # sign indicates to the compiler that it's a literal string, and therefore nothing will be escaped.
string filename = "te\test";
string[] x = filename.Split('\\');
Console.WriteLine(filename);//this line should be Console.WriteLine(x[0]+X[1]);
Console.ReadLine();
But I think you are looking for
filename = filename.Replace("\\","/");
Console.WriteLine(filename);

String Replace Line Breaks

I have read an entire file into a string object :-
string result = File.ReadAllText(#"C:\TestLog.log");
I want to remove all line breaks, "\n", but i also need to preserve any instances in the string object where "\r\n" exists.
How can i do this?
It looks like you want a regular expression replace.
string result = File.ReadAllText(#"C:\TestLog.log");
string newresult = Regex.Replace(result, #"[^\\r]\\n", "");
So the pattern looks for any \n that is not preceded by \r.
result = result.Replace("\n","").Replace("\r","\r\n")
Without using regex though. Not sure if you intend to use that or not.

Trim string by strings

How can I trim a string by a whole string instead of a list of single characters?
I want to remove all and whitespaces at beginning and end of an HTML string. But method String.Trim() does only have overloads for set of characters and not for set of strings.
You could use HttpUtility.HtmlDecode(String) and use the resultant as an input for String.Trim()
HttpUtility.HtmlDecode on MSDN
HttpServerUtility.HtmlDecode on MSDN (a wrapper you can access through the Page.Server property)
string stringWithNonBreakingSpaces;
string trimmedString = String.Trim(HttpUtility.HtmlDecode(stringWithNonBreakingSpaces));
Note: This solution would decode all the HTML strings in the input.
The Trim method removes from the current string all leading and trailing white-space characters by default.
Edit: Solution for your problem AFTER your edit:
string input = #" <a href='#'>link</a> ";
Regex regex = new Regex(#"^( |\s)*|( |\s)*$");
string result = regex.Replace(input, String.Empty);
This will remove all trailing and leading spaces and . You can add any string or character group to the expression. If you were to trim all tabs too the regex would simply become:
Regex regex = new Regex(#"^( |\s|\t)*|( |\s|\t)*$");
Not sure if this is what you're looking for?
string str = "hello ";
str.Replace(" ", "");
str.Trim();
Use RegEx, as David Heffernan said. It is rather easy to select all spaces at the start of string: ^(\ | )*

problem in regular expression

I am having a regular expression
Regex r = new Regex(#"(\s*)([A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|Y|X]\d(?!.*[DFIOQU])(?:[A-Z](\s?)\d[A-Z]\d))(\s*)",RegexOptions.IgnoreCase);
and having a string
string test="LJHLJHL HJGJKDGKJ JGJK C1C 1C1 LKJLKJ";
I have to fetch C1C 1C1.This running fine.
But if a modify test string as
string test="LJHLJHL HJGJKDGKJ JGJK C1C 1C1 ON";
then it is unable to find the pattern i.e C1C 1C1.
any idea why this expression is failing?
You have a negative look ahead:
(?!.*[DFIOQU])
That matches the "O" in "ON" and since it is a negative look ahead, the whole pattern fails. And, as an aside, I think you want to replace this:
[A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|Y|X]
With this:
[A-CEGHJ-NPR-TVYX]
A pipe (|) is a literal character inside a character class, not an alternation, and you can use ranges to help hilight the characters that you're leaving out.
A single regex might not be the best way to parse that string. Or perhaps you just need a looser regex.
You are searching for a not a following DFIOQU with your negative look ahead (?!.*[DFIOQU])
In your second string there is a O at the end in ON, so it must be failing to match.
If you remove the .* in your negative look ahead it will only check the directly following character and not the complete string to the end (Is it this what you want?).
\s*([ABCEGHJKLMNPRSTVYX]\d(?![DFIOQU])(?:[A-Z]\s?\d[A-Z]\d))\s*
then it works, see it here on Regexr. It is now checking if there is not one of the characters in the class directly after the digit, I don't know if this is intended.
Btw. I removed the | from your first character class, its not needed and also some brackets around your whitespaces, also not needed.
As I understood you need to find the C1C 1C1 text in your string
I've used this regex for do this
string strRegex = #"^.*(?<c1c>C1C)\s*(?<c1c2>1C1).*$";
after that you can extract text from named groups
string strRegex = #"^.*(?<c1c>C1C)\s*(?<c1c2>1C1).*$";
RegexOptions myRegexOptions = RegexOptions.Multiline;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = #"LJHLJHL HJGJKDGKJ JGJK C1C 1C1 LKJLKJ";
string secondStr = "LJHLJHL HJGJKDGKJ JGJK C1C 1C1 ON";
Match match = myRegex.Match(strTargetString);
string c1c = match.Groups["c1c"].Value;
string c1c2 = match.Groups["c1c2"].Value;
Console.WriteLine(c1c + " " +c1c2);

Remove characters using Regex

I have a string. I need to replace all instances of a given array of strings from this original string - how would I do that?
Currently I am using...
var inputString = "this is my original string.";
var replacement = "";
var pattern = string.Join("|", arrayOfStringsToRemove);
Regex.Replace(inputString, pattern, replacement);
This works fine, but unfortunately it breaks down when someone tries to remove a character that has a special meaning in the regex.
How should I do this? Is there a better way?
Build the pattern using Regex.Escape:
StringBuilder pattern = new StringBuilder();
foreach (string s in arrayOfStringsToRemove)
{
pattern.Append("(");
pattern.Append(Regex.Escape(s));
pattern.Append(")|");
}
Regex.Replace(inputString, pattern.ToString(0, pattern.Length - 1), // remove trailing |
replacement);
Look at Regex.Escape
You need to escape special characters with a backslash
\
Sometimes you may need to use two backslashes
\\
You need to escape characters with spacial meaning of course.
var str_to_replace = "removing \[this\]\(link\)";

Categories