Just trying out C# to make a button that loads csv files verify them and parse them:
protected void Upload_Btn_Click(object sender, EventArgs e)
{
string test = PNLdataLoader.FileName;
//checks if file is csv
Regex regex = new Regex("*.csv");
Match match = regex.Match(test);
if (match.Success)
{
string CSVFileAsString = System.Text.Encoding.ASCII.GetString(PNLdataLoader.FileBytes);
System.IO.MemoryStream MS = new System.IO.MemoryStream(PNLdataLoader.FileBytes);
System.IO.StreamReader SR = new System.IO.StreamReader(MS);
//Store each line in CSVlines array of strings
string[] CSVLines = new string[0];
while (!SR.EndOfStream)
{
System.Array.Resize(ref CSVLines, CSVLines.Length + 1);
CSVLines[CSVLines.Length - 1] = SR.ReadLine();
}
}
So far I got it to store the lines in CSVLines but I am not sure what is wrong with the regex. Is there a more efficient way to do this?
That isn't a valid expression, its saying match whatever character that comes before * 0 or more times, since there is no character before that there is a problem.
This will probably match most things, it does not include special characters.
Regex regex = new Regex("[a-zA-Z0-9]{1,}.csv");
You could also do this instead:
if(test.EndsWith(".csv"))
and lastly, I would change your array to a List<T> or something like that, futher explained here: What is more efficient: List<T>.Add() or System.Array.Resize()?
//Store each line in CSVlines array of strings
List<string> CSVLines = new List<string>();
while (!SR.EndOfStream)
{
CSVLines.Add(SR.ReadLine());
}
EDIT:
List<T> is in System.Collections.Generic
Related
Alright, I have a program that grabs links off of a website and puts it into a txt BUT the links aren't separated onto their own lines and I need to somehow do that without having to manually do it myself, here is the code used to grab the links off of the website, write the links to a text file then grab the txt file and read it.
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
var client = new WebClient();
string text = client.DownloadString("https://currentlinks.com");
File.WriteAllText("C:/ProgramData/oof.txt", text);
string searchKeyword = "https://foobar.to/showthread.php";
string fileName = "C:/ProgramData/oof.txt";
string[] textLines = File.ReadAllLines(fileName);
List<string> results = new List<string>();
foreach (string line in textLines)
{
if (line.Contains(searchKeyword))
{
results.Add(line);
}
var sb = new StringBuilder();
foreach (var item in results)
{
sb.Append(item);
}
textBox1.Text = sb.ToString();
var parsed = textBox1;
TextWriter tw = new StreamWriter("C:/ProgramData/parsed.txt");
// write lines of text to the file
tw.WriteLine(parsed);
// close the stream
tw.Close();
}
}
You are getting all the Links (URLs) in one single string. There is not straight forward way to get all the URLs individually without some assumptions.
With the sample data you shared, I assume that the URLs in the string follow simple URLs format and do not have any fancy stuff in it. They start with http and one url does not have any other http.
With above assumptions, I suggest following code.
// Sample data as shared by the OP
string data = "https://forum.to/showthread.php?tid=22305https://forum.to/showthread.php?tid=22405https://forum.to/showthread.php?tid=22318";
//Splitting the string by string `http`
var items = data.Split(new [] {"http"},StringSplitOptions.RemoveEmptyEntries).ToList();
//At this point all the strings in items collection will be without "http" at the start.
//So they will look like as following.
// s://forum.to/showthread.php?tid=22305
// s://forum.to/showthread.php?tid=22405
// s://forum.to/showthread.php?tid=22318
//So we need to add "http" at the start of each of the item as following.
items = items.Select(i => "http" + i).ToList();
// After this they will become like following.
// https://forum.to/showthread.php?tid=22305
// https://forum.to/showthread.php?tid=22405
// https://forum.to/showthread.php?tid=22318
//Now we need to create a single string with newline character between two items so
//that they represent a single line individually.
var text = String.Join("\r\n", items);
// Then write the text to the file.
File.WriteAllText("C:/ProgramData/oof.txt", text);
This should help you resolve your issue.
.Split way
Could you use yourString.Split("https://");?
Example:
//This simple example assumes that all links are https (not http)
string contents = "https://www.example.com/dogs/poodles/poodle1.htmlhttps://www.example.com/dogs/poodles/poodle2.html";
const string Prefix = "https://";
var linksWithoutPrefix = contents.Split(Prefix, StringSplitOptions.RemoveEmptyEntries);
//using System.Linq
var linksWithPrefix = linksWithoutPrefix.Select(l => Prefix + l);
foreach (var match in linksWithPrefix)
{
Console.WriteLine(match);
}
Regex way
Another option is to use reg exp.
Failed - cannot find/write the right regex ... got to go now
string contents = "http://www.example.com/dogs/poodles/poodle1.htmlhttp://www.example.com/dogs/poodles/poodle2.html";
//From https://regexr.com/
var rgx = new Regex(#"(?<Protocol>\w+):\/\/(?<Domain>[\w#][\w.:#]+)\/?[\w\.?=%&=\-#/$,]*");
var matches = rgx.Matches(contents);
foreach(var match in matches )
{
Console.WriteLine(match);
}
//This finds 'http://www.example.com/dogs/poodles/poodle1.htmlhttp' (note the htmlhttp at the end
I have this piece of code which repeats about 250,000 on a loop searching through the records. There are 28 different Regex (this being one of them). Is there an easier way other than writing to a file, reading it into a string and using each towards the end of my code?
if (CSV_Radio_Button.Checked)
{
string pattern = #"(?<=\*\*Course : )(.*?)(?=\*\*Going)";
Regex myRegex = new Regex(pattern, RegexOptions.IgnoreCase);
Match m = myRegex.Match(text);
while (m.Success)
{
string CourseToString = m.ToString();
System.IO.File.WriteAllText(CourseFile, UppercaseWords(CourseToString));
m = m.NextMatch();
}
}
string Course = File.ReadLines(CourseFile).ElementAtOrDefault(0);
I haven't tested this, but this is how I've done something similar. A list would work but you'd be iterating over the list when you are done to build the string. You could also use one of the various Streams.
StringBuilder CourseBuilder = new StringBuilder();
while (m.Success)
{
CourseBuilder.AppendLine(m.ToString());
m = m.NextMatch();
}
}
string Course = CourseBuilder.ToString();
If you intentionally overwriting file here:
System.IO.File.WriteAllText(CourseFile, UppercaseWords(CourseToString));
you can replace that line with string defined before your block like this:
string CSV_regex_result;
if (CSV_Radio_Button.Checked)
{
...
while(m.Success) {
CSV_regex_result = UppercaseWords(m.ToString());
m = m.NextMatch();
}
}
Now you can access last matched regex in CSV_regex_result.
If there is mistake in code and you want all regex it depends, if you want it separated or not.
If you want single string David Green answer is way to go. But be careful about string size limit.
If you want separated results:
Replace in my example string CSV_regex_result; with List<string> CSV_regex_result = new List<string>(); and in loop replace CSV_regex_result = UppercaseWords(m.ToString()); with CSV_regex_result.Add(UppercaseWords(m.ToString()));.
If you want results accessible separated by regex name. You can:
Dictionary<string, List<string>> result = new Dictionary<string, List<string>>();
...
List<string> Course_result = new List<string>();
...
//in loop
Course_result.Add(UppercaseWords(m.ToString()));
...
//after loop
if (!result.ContainsKey("Course")) result.Add("Course",Course_result);
else result["Course"]=Course_result;
Of course if you want merged results of regex you can create Dictionary<string,string> and add results generated with StringBuilder.
In case will run out of memory (depends on your memory size and data amount) it can be good to stick with your current approach (save parts to files).
I´ve got a text file with tabulator separated data. What I need in my C# application is that I read one line from the text file and save them to an array, separate them at the each \t. Then I do the same thing with the next row.
My code:
StreamReader sr = new StreamReader(dlg.FileName);
string s = sr.ReadLine();
Now, I already tried to write the line into an array but that doesn´t work. Does anyone one how to manage this?
Use the Split method to create an Array of the line
string[] parts = s.Split('\t');
See Documentation on Split() here
foreach (string line in System.IO.File.ReadAllLines(dlg.FileName))
{
var myArray = line.Split('\t');
}
s.Split('\t') will split your string by the tabulator character, and create a string[] with appropriate length.
Ammending your example code:
StreamReader sr = new StreamReader(dlg.FileName);
string s = sr.ReadLine();
var items = s.Split('\t');
In the end, items contains an array of strings that represent the characters between the tabs. The tabs are not included in the array. The array may contain empty elements (for the case of two consecutive tabs).
Use the String.Split() method: http://msdn.microsoft.com/en-us/library/b873y76a.aspx
StreamReader reader = new StreamReader("input.txt");
string[] content = reader.ReadToEnd().Replace("\n","").Split('\t');
if you want to keep New Line's than
string[] content = reader.ReadToEnd().Split('\t');
In the example below, items will be a String[] containing each line of text's values. It will be overwritten with each iteration so you'll want to do something with it inside the loop. Save it to a larger collection, write it to a file, etc...
StreamReader sr = new StreamReader(dlg.FileName);
while (sr.Peek() >= 0) {
var line = sr.ReadLine();
var items = line.Split(new Char[] { '\t' });
}
If the file contains only one line, then use:
string[] myArray = s.Split('\t');
Am trying to get values from a text file in which entries are delimited using '|'.am getting the values using string .Split method..but in some places the delimiter appears multiple times in succession like '||||||||',so empty space gets inserted in the array how should i remove those empty elements from array or is there any efficient technique to read values from text file delimited by '|".below is my code and the screen shot of array values
var reader = new StreamReader(File.OpenRead(#"d:\er.txt"));
while (!reader.EndOfStream)
{
var line = reader.ReadLine().Trim();
var values = line.Split('|');
string[] ee = values;
}
can any one suggest a better method for reading data from text file delimited by '|'
Split has an overload that takes a StringSplitOptions enumeration value:
var values = line.Split(new char[]{'|'}, StringSplitOptions.RemoveEmptyEntries);
This will remove empty entries.
You can use linq on your array?
var values = line.Split("|").Where(v => !string.IsNullOrEmpty(v));
See, the below line of codes, you can do something like that.
var values = line.Split('|');
List<string> FilteredValues = new List<string>();
foreach (var value in values)
{
if (value != "")
{
FilteredValues.Add(value);
}
}
further to Oded's answer this is the correct syntax
var reader = new StreamReader(File.OpenRead(#"d:\er.txt"));
while (!reader.EndOfStream)
{
var line = reader.ReadLine().Trim();
var values = line.Split(new char[]{'|'}, StringSplitOptions.RemoveEmptyEntries);
string[] ee = values;
}
Or, maybe, you can use the trim() function !?
Basically I'm trying to read a really big text file and when the charecters of the line reach X amount write to a new line, but I can't seem to get the character count to work. Any help is appreciated!
using (FileStream fs = new FileStream(betaFilePath,FileMode.Open))
using (StreamReader rdr = new StreamReader(fs))
{
while (!rdr.EndOfStream)
{
string betaFileLine = rdr.ReadLine();
int stringline = 0;
if (betaFileLine.Contains("þTEMP"))
{
//sb.AppendLine(#"C:\chawkster\workfiles\New Folder\GEL_ALL_PRODUCTS_CONCORD2.DAT");
string checkline = betaFileLine.Length.ToString();
foreach (string cl in checkline)
{
stringline++;
File.AppendAllText(#"C:\chawkster\workfiles\New Folder\GEL_ALL_PRODUCTS_CONCORD3.DAT", cl);
if(stringline == 1200)
{
File.AppendAllText(#"C:\chawkster\workfiles\New Folder\GEL_ALL_PRODUCTS_CONCORD3.DAT","\n");
stringline = 0;
}
}
}
}
Error:
foreach (string cl in checkline)
Error 1 Cannot convert type 'char' to 'string'
I don't understand why you have string checkline = betaFileLine.Length.ToString(); since that will just take the current line and give you the length which is a number in a string format. Don't you want all the characters in the current line? Not sure what you want the numeric length there.
Not really sure what you are doing exactly but try:
// Get the current line as an array of characters
char[] checkline = betaFileLine.ToCharArray();
// Iterate for each character add to you file?
foreach (char cl in checkline)
I would use a Regular Expression to split the input string into chunks of the desired amount of characters. Here's an example:
string input = File.ReadAllText(inputFilePath);
MatchCollection lines = Regex.Matches(input, ".{1200}", RegexOptions.Singleline); // matches any character including \n exactly 1200 times
StringBuilder output = new StringBuilder();
foreach (Match line in lines)
{
output.AppendLine(line.Value);
}
File.AppendAllText(outputFilePath, output.ToString());
System.String implements an IEnumerable - you need to use the code
foreach (char cl in checkLine)
{
...
File.AppendAllText(fileName, cl.ToString());
}
I'd also suggest you put it all into an in-memory stream or StringBuilder and persist it all to the file in one go, rather than writing each character to the FileStream separately.