Searching the inbetweens?
-
Hi all!, I have a weird question and some code that isn't quite right. :( I am creating a personal project where I have an already-existing text file which consists of mainly symbols and standard text as follows: Sample Text File contents: http://www.my_soon-to-be_homepage.com.au/ This file may contain X number of lines, and what I am trying to do is search for the in-betweens, what I mean by that is I want to be able to search for the keywords (human, being, person, hairy, funny, intelligent etc..) that are inbetween the symbols and then put those found keywords into a string so that i can cut, copy, paste, change them at my own will. Then I need to be able to search for a URL that is within that same text file. And then put that URL into a string so I can cut, copy, paste etc..... into a textBox. I have some code here that searches but it doesn't work the way i want it to. I'd appreciate any samps, tutorials, advice/ or ANY help i can get from anybody. Thanks for taking the time to read this. I hope it wasn't too confusing.. :-D best regards, j.t. j.t.
-
Hi all!, I have a weird question and some code that isn't quite right. :( I am creating a personal project where I have an already-existing text file which consists of mainly symbols and standard text as follows: Sample Text File contents: http://www.my_soon-to-be_homepage.com.au/ This file may contain X number of lines, and what I am trying to do is search for the in-betweens, what I mean by that is I want to be able to search for the keywords (human, being, person, hairy, funny, intelligent etc..) that are inbetween the symbols and then put those found keywords into a string so that i can cut, copy, paste, change them at my own will. Then I need to be able to search for a URL that is within that same text file. And then put that URL into a string so I can cut, copy, paste etc..... into a textBox. I have some code here that searches but it doesn't work the way i want it to. I'd appreciate any samps, tutorials, advice/ or ANY help i can get from anybody. Thanks for taking the time to read this. I hope it wasn't too confusing.. :-D best regards, j.t. j.t.
If you make your file XML you can use an XML reader to access the attribute values. (The values in quotes)
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego. -
Hi all!, I have a weird question and some code that isn't quite right. :( I am creating a personal project where I have an already-existing text file which consists of mainly symbols and standard text as follows: Sample Text File contents: http://www.my_soon-to-be_homepage.com.au/ This file may contain X number of lines, and what I am trying to do is search for the in-betweens, what I mean by that is I want to be able to search for the keywords (human, being, person, hairy, funny, intelligent etc..) that are inbetween the symbols and then put those found keywords into a string so that i can cut, copy, paste, change them at my own will. Then I need to be able to search for a URL that is within that same text file. And then put that URL into a string so I can cut, copy, paste etc..... into a textBox. I have some code here that searches but it doesn't work the way i want it to. I'd appreciate any samps, tutorials, advice/ or ANY help i can get from anybody. Thanks for taking the time to read this. I hope it wasn't too confusing.. :-D best regards, j.t. j.t.
Either use the System.XML namespace thus formatting your document to use XML standard formatting, either use the System.Text.RegularExpressions namespace similar to: System.Net.RegularExpressions reg = new System.Net.RegularExpressions.Regex("<[a-z]+=""([a-z,\s]+)"" \/>"); Use expresso to get the expression that best fits ur needs. The above is just a shallow example.
-
Either use the System.XML namespace thus formatting your document to use XML standard formatting, either use the System.Text.RegularExpressions namespace similar to: System.Net.RegularExpressions reg = new System.Net.RegularExpressions.Regex("<[a-z]+=""([a-z,\s]+)"" \/>"); Use expresso to get the expression that best fits ur needs. The above is just a shallow example.
Oh, sorry, I forgot to post the code I mentioned in my Question. Thanks for helping me, both of you :-) It's much appreciated:
using System.IO;
using System.Text.RegularExpressions;private void SearchLink\_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { if( File.Exists("C:\\\\Users\\\\Jase\\\\Desktop\\\\Test.txt")) { // File to search. string FileName = "C:\\\\Users\\\\Jase\\\\Desktop\\\\Test.txt"; // Reads file to end. StreamReader SearchText = new StreamReader(FileName); string ReadAll = SearchText.ReadToEnd(); // SearchTextBox.Text contains string to search for. SearchText.Close(); string regMatch = SearchTextBox.Text; // If the string has been found, do something. if (Regex.IsMatch(ReadAll, regMatch)) { MessageBox.Show("Found\\n" + "" + regMatch); } else { MessageBox.Show("Not found\\n"); } } else { MessageBox.Show("Non-Existant File."); } }
The only problem with this code is it requires somebody to enter text into a textbox and then it will search for that text in an existing file. I actually need to just search for anything that is inbetween two quotes ("sample thing"). I tried both of your suggestions and I got a little confused and it didn't work, so I went to MSDN before posting another question here and I left MSDN even more confused. Thanks for your quick replies :-)
j.t.
-
Oh, sorry, I forgot to post the code I mentioned in my Question. Thanks for helping me, both of you :-) It's much appreciated:
using System.IO;
using System.Text.RegularExpressions;private void SearchLink\_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { if( File.Exists("C:\\\\Users\\\\Jase\\\\Desktop\\\\Test.txt")) { // File to search. string FileName = "C:\\\\Users\\\\Jase\\\\Desktop\\\\Test.txt"; // Reads file to end. StreamReader SearchText = new StreamReader(FileName); string ReadAll = SearchText.ReadToEnd(); // SearchTextBox.Text contains string to search for. SearchText.Close(); string regMatch = SearchTextBox.Text; // If the string has been found, do something. if (Regex.IsMatch(ReadAll, regMatch)) { MessageBox.Show("Found\\n" + "" + regMatch); } else { MessageBox.Show("Not found\\n"); } } else { MessageBox.Show("Non-Existant File."); } }
The only problem with this code is it requires somebody to enter text into a textbox and then it will search for that text in an existing file. I actually need to just search for anything that is inbetween two quotes ("sample thing"). I tried both of your suggestions and I got a little confused and it didn't work, so I went to MSDN before posting another question here and I left MSDN even more confused. Thanks for your quick replies :-)
j.t.
IsMatch returns a boolean whether it found or not your regex expression in the input string. The correct way to use regex would be with this basic regex Regex reg = New Regex("""(.+?)""", RegexOptions.IgnoreCase | RegexOptions.Multiline); MatchCollection mtc = reg.Matches("input string"); this will return a MatchCollection which u can loop through to get every match of the pattern in the input string. Use the GetEnumerator method of the above MatchCollection to loop through the matches.
Code? Yeah i love it fried together with a glass of wine.