How can I replace a unknown string in a file? - c#

Currently I'm writing a library to make reading and writing INI files simple, I have got the reader working and writing when the key and value doesn't exist but I cannot update the value easily, I have tried various methods to replace the string however none are practical and the program requires the old value
Here is an example I have tried from here: Regular Expression to replace unknown value in text file - c# and asp.net
Regex rgx = new Regex(#"SQL-SERVER-VERSION="".*?""");
string result = rgx.Replace(input, replacement);
But every time I modify that code to replace the value it ends up replacing the key instead thus resulting in an error when the app tries to read the file next time.
Here is the code I am using currently:
private string wholedata;
private void UpdateKey(string key, string path, string newval)
{
try
{
using (StreamReader s = new StreamReader(File.Open(path, FileMode.Open)))
{
wholedata = s.ReadToEnd();
Regex rgx = new Regex(key + ".*?");
string result = rgx.Replace(wholedata, newval);
MessageBox.Show(result);
}
using (StreamWriter s = new StreamWriter(File.Open(path, FileMode.OpenOrCreate)))
{
s.Write(wholedata);
}
}
catch (Exception e)
{
}
}

Why rewriting what the system already know how to do? Take a look at this project : An INI file handling class using C#. You should also be aware that Xml has taken a great place in the .Net framework. It's not uncommon to use either the configuration framework or a simple settings object that you serialize/deserialize. I don't know you requirement, but it can enlight us to describe a bit why you want to use ini files.
That said, you are writing the old value (wholedata), not the new value (result). This may be be the root cause.
When you call Regex.Replace (so do string.Replace), it actually generate a new string, and does not change the string passed in parameters.

Change the regex to
Regex rgx = new Regex(#"(?<=SQL-SERVER-VERSION="").*?(?="")");
This will match the .*? on the proviso that the prefix and suffix exist ( (?<=) and (?=) groupings)

Related

Parse Line and Break it into Variables

I have a text file that contain only the FULL version number of an application that I need to extract and then parse it into separate Variables.
For example lets say the version.cs contains 19.1.354.6
Code I'm using does not seem to be working:
char[] delimiter = { '.' };
string currentVersion = System.IO.File.ReadAllText(#"C:\Applicaion\version.cs");
string[] partsVersion;
partsVersion = currentVersion.Split(delimiter);
string majorVersion = partsVersion[0];
string minorVersion = partsVersion[1];
string buildVersion = partsVersion[2];
string revisVersion = partsVersion[3];
Altough your problem is with the file, most likely it contains other text than a version, why dont you use Version class which is absolutely for this kind of tasks.
var version = new Version("19.1.354.6");
var major = version.Major; // etc..
What you have works fine with the correct input, so I would suggest making sure there is nothing else in the file you're reading.
In the future, please provide error information, since we can't usually tell exactly what you expect to happen, only what we know should happen.
In light of that, I would also suggest looking into using Regex for parsing in the future. In my opinion, it provides a much more flexible solution for your needs. Here's an example of regex to use:
var regex = new Regex(#"([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9])");
var match = regex.Match("19.1.354.6");
if (match.Success)
{
Console.WriteLine("Match[1]: "+match.Groups[1].Value);
Console.WriteLine("Match[2]: "+match.Groups[2].Value);
Console.WriteLine("Match[3]: "+match.Groups[3].Value);
Console.WriteLine("Match[4]: "+match.Groups[4].Value);
}
else
{
Console.WriteLine("No match found");
}
which outputs the following:
// Match[1]: 19
// Match[2]: 1
// Match[3]: 354
// Match[4]: 6

Importing Data from a csv file

I have a csv file.
When I try to read that file using filestream readtoend(), I get inverted commas and \r at many places that breaks my number of rows in each column.
Is there a way to remove inverted commas and \r.
I tried to replace
FileStream obj = new FileStream();
string a = obj.ReadToEnd();
a.Replace("\"","");
a.Replace("\r\"","");
When I visualize a all \r and inverted commas are removed.
But when I read the file again from beginning using ReadLine() they appear again?
First of all, a String is immutable. You might think this is not important for your question, but actualy it's important whenever you are developing.
If I look at your code snippet, I'm pretty sure you have no knowledge of immutable objects so I advice you to make sure you fully understand the concept.
More information regarding immutable objects can be found: http://en.wikipedia.org/wiki/Immutable_object
Basicly, it means one can never modify a string object. Strings will always point to a new object whenever we change the value.
That's why the Replace method returns a value, which's documentation can be found here: https://msdn.microsoft.com/en-us/library/system.string.replace%28v=vs.110%29.aspx and states clearly that it Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
In your example, you aren't using the return value of the Replace function.
Could you show us that the string values are actuably being replaced from your a variable? Because I do not believe this is going to be the case. When you visualize a string, carriage returns (\r) are not visual and replaced by an actual carriage return. If you debug and take alook at the actual string value, you should still see the \n.
Take the following code snippet:
var someString = "Hello / world";
someString.Replace("/", "");
Console.Log(someString);
You might think that the console will show "Hello world". However, on this fiddle you can see that it still logs "Hello / World": https://dotnetfiddle.net/cp59i3
What you have to do to correctly use String.Replace can be seen in this fiddle: https://dotnetfiddle.net/XCGtOu
Basicly, you want to log the return value of the Replace function:
var a = "Some / Value";
var b = a.Replace("/", "");
Console.WriteLine(b);
Also, as mentioned by others in the comment section at ur post, you are not replacing the contents of the file, but the string variable in your memory.
If you want to save the new string, make sure to use the Write method of the FileStream (or any other way to write to a file), an explanation can be found here: How to Find And Replace Text In A File With C#
Apart from all what I have been saying throughout this answer, you should not replace both inverted comma's and carriage returns in a file in most cases, they are there for a reason. Unless you do have a specific reason.
At last I succeeded. Thanks to everybody. Here is the code I did.
FileStream obj = new FileStream();
using(StreamReader csvr = new StreamReader(obj))
{
string a = obj.ReadToEnd();
a = a.Replace("\"","");
a = a.Replace("\r\"","");
obj.Dispose();
}
using(StreamWriter Wr = new StreamWriter(TempPath))
{
Wr.Write(a);
}
using(StreamReader Sr = new StreamReader(Tempath))
{
Sr.ReadLine();
}
I Created a temp path on the system. After this things were easy to enter into database.
Try something like this
StreamReader sReader = new StreamReader("filename");
string a = sReader.ReadToEnd();
a.Replace("\"", "");
a.Replace("\r\"", "");
StringReader reader = new StringReader(a);
string inputLine = "";
while ((inputLine = reader.ReadLine()) != null)
{
}

Split string by comma only when is coma outside of double-quotes c#

"TIMESTAMP (UTC)","LOG TYPE","DEVICE TYPE","DEVICE","MESSAGE","PARAMETERS"
"2014-08-12 17:30:34.437","Warning","DiverGate","141403G00294","Diver gate(s) did not connect since","2014-08-08 06:37:31 (UTC)"
"2014-08-12 17:30:34.577","Warning","DiverGate","141403G00120","Diver gate(s) did not connect since","2014-08-08 06:46:22 (UTC)"
"2014-08-13 06:45:18.890","Error","DiverGate","141403G00294","Was set to inactive, because it did not connect since","2014-08-08 06:37:31 (UTC)"
"2014-08-13 07:00:18.903","Error","DiverGate","141403G00120","Was set to inactive, because it did not connect since","2014-08-08 06:46:22 (UTC)"
This is my .csv file and i need to read informations from file, but I need to split informations with comma who is outside double quotes, because in some other files I can find comma into some informations, especially in message, log type,...
string url = #"E:\Project.csv";
Stream stream = File.Open(url, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
string[] lines = null;
using (StreamReader sr = new StreamReader(stream))
{
string str = sr.ReadToEnd();
lines = Regex.Split(str, //what expression is going here);
}
You can try with Lookaround
They do not consume characters in the string, but only assert whether a match is possible or not.
(?<="),(?=")
Here is online demo and tested at regexstorm
Pattern explanation is very simple
(?<= look behind to see if there is:
" '"'
) end of look-behind
, ','
(?= look ahead to see if there is:
" '"'
) end of look-ahead
This is just basic CSV parsing, and there are libraries out there to do it already. I would recommend taking a look at CsvHelper which I've used before rather than trying to re-invent the wheel.
You can include this in your project really easily by using the Package Manager Console and typing:
Install-Package CsvHelper
Instead of rolling out your own CSV parser, use existing libraries. There is TextFieldParser class which is available with Visual Basic, Just add reference to Microsoft.VisualBasic under project references then you can do:
TextFieldParser textFieldParser = new TextFieldParser(#"E:\Project.csv");
textFieldParser.TextFieldType = FieldType.Delimited;
textFieldParser.SetDelimiters(",");
while (!textFieldParser.EndOfData)
{
string[] values = textFieldParser.ReadFields();
Console.WriteLine(string.Join("---", values));//printing the row
}
textFieldParser.Close();
Hey you can also use this regex
var result = Regex.Split(samplestring, ",(?=(?:[^']*'[^']*')*[^']*$)");

How to replace > 1 tokens on a string?

I have a list of tokens
[#date]
[#time]
[#fileName]
... etc
That are dispersed all over a large file. I can parse the file and replace them with Regex.Replace easily when there's only one token on a line. However the problem arises when there's two tokens on one line
example:
[#date] [#time]
What I thought about doing is using String.Split with " " as the delimiter, and then iterate through the result checking if there are tokens.
But I see two problems with this approach, the file is rather large and this would definitely impact performance. The second problem is that the file that will be outputted is a SQL file and I'd like to retain the white space just for looks.
Is there a more elegant solution to this problem? Or is it just another case of premature optimization?
One thing you can do is that instead of replacing patterns line by line, replace them in the whole file:
string fileContent = File.ReadAllText(path);
fileContent = Regex.Replace(fileContent, pattern1, replacement1);
...
fileContent = Regex.Replace(fileContent, patternN, replacementN);
One simple way to do this is to store tokens and their values separately and then to iterate over them replacing your query with values for that tokens. Example is given below:
var tokensWithValues = new Dictionary<string, object>()
{
{"[#date]", DateTime.Now},
{"[#time]", DateTime.Now.Ticks},
{"[#fileName]", "myFile.xml"},
};
var sqlQuery = File.ReadAllText("mysql.sql");
foreach (var tokenValue in tokensWithValues)
{
sqlQuery = sqlQuery.Replace(tokenValue.Key, tokenValue.Value.ToString());
}
try using the string.Replace(...) extension. That would allow you to Replace all instances of a string.
for example
string file = File.ReallAllText("myfile.txt");
file.Replace("[#date]", "replaced_value");
the above would replace all instances of "[#date]" with "replaced_value".
Edited as previous answer included custom extensions not available to OP. Thanks llya.

How can I efficiently process a delimited text file?

I'm simply trying to execute File.ReadAllLines against a specific file and, for every line, split on |. I have to use regex on this one.
This code below doesnt work, but you'll see what i'm trying to do:
string[] contents = File.ReadAllLines(filename);
string[] splitlines = Regex.Split(contents, '|');
foreach (string split in splitlines)
{
//Regex line = content.Split('|');
//content.Split('|');
string prefix = prefix = Regex.Match(line, #"(\S+)(\d+)").Groups[0].Value;
File.AppendAllText(workingdirform2 + "configuration.txt", prefix+"\r\n");
}
It's not entirely clear to me what you are trying to do, but there are a number of errors in your code. I have tried to guess what you are doing, but if this isn't what you want, please explain what you do want preferably with some examples:
string inputFilename = "input.txt";
string outputFilename = "output.txt";
using (StreamWriter streamWriter = File.AppendText(outputFilename))
{
using (StreamReader streamReader = File.OpenText(inputFilename))
{
while (true)
{
string line = streamReader.ReadLine();
if (line == null)
{
break;
}
string[] splitlines = line.Split('|');
foreach (string split in splitlines)
{
Match match = Regex.Match(split, #"\S+\d+");
if (match.Success)
{
string prefix = match.Groups[0].Value;
streamWriter.WriteLine(prefix);
}
else
{
// Handle match failed...
}
}
}
}
}
Key points:
You seem to want to perform an operation on each line, so you need to iterate over the lines.
Use the simple string.Split method if you want to split on a single character. Regex.Split doesn't accept a character and "|" has a special meaning in regular expressions so it wouldn't have worked anyway unless you escaped it.
You were opening and closing the output file multiple times. You should open it just once and keep it open until you have finished writing to it. The using keyword is useful here.
Use WriteLine instead of appending "\r\n".
If the input file is large, use a StreamReader instead of ReadAllLines.
If the match fails, your program will throw an exception. You probably should check match.Success before using the match and if this returns false, handle the error appropriately (skip the line, report a warning, throw an exception with an appropriate message, etc.)
You aren't actually using groups 1 and 2 in the regular expression, so you can remove the parentheses to save the regular expression engine from having to store results that you won't use anyway.
You should pass the original string to Regex.Split and not an array.
Looks like you are using line instead of split when settings the prefix. Without knowing more about your code I cant tell if it's right or not but in any case it sticks out as the error.(it shouldnt build either)
This is a really inefficient on at least two levels :)
Regex.Split takes a string, not an array of strings.
I would recommend calling Regex.Split on each item of contents individually, then looping over the results of that call. This would mean nested for loops.
string[] contents = File.ReadAllLines(filename);
foreach (string line in contents)
{
string[] splitlines = Regex.Split(line);
foreach (string splitline in splitlines)
{
string prefix = Regex.Match(splitline, #"(\S+)(\d+)").Groups[0].Value;
File.AppendAllText(workingdirform2 + "configuration.txt", prefix+"\r\n");
}
}
This, of course isn't the most efficient way to go about it.
A more efficient way might be to split on a regular expression instead. I think this works:
string splitlines = Regex.Split(File.ReadAllText(filename), "$|\\|");
I have to assume, based on the limited feedback, that this is what you're looking for:
string inputFile = filename;
string outputFile = Path.Combine( workingdirform2, "configuration.txt" );
using ( StreamReader inputFileStream = File.OpenText( inputFile ) )
{
using ( StreamWriter ouputFileStream = File.AppendText( outputFile ) )
{
// Iterate over the file contents to extract the prefix
string currentLine;
while ( ( currentLine = inputFileStream.ReadLine() ) != null )
{
// Notice the updated Regex - your's is a bit broken
string prefix = Regex.Match( currentLine, #"^(\S+?)\d+" ).Groups[1].Value;
ouputFileStream.WriteLine( prefix );
}
}
}
This would take a file full of:
Text1231|abc|abc
Text1232|abc|abc
Text1233|abc|abc
Text1234|abc|abc
and place:
Text
Text
Text
Text
into a new file.
I hope this, at least, gets you on the right path. My crystal ball is getting hazy.. haaazzzy..
Probably one of the best way to process text files in C# is to use fileHelpers. Give it a look. It allows you to strongly type your import data.

Categories

Resources