Hey Everybody!
So say i have a file that kinda looks like this...
[System]
public class
STUFF
STUFF
STUFF
STUFF
end of class
[System]
public class1
STUFF
STUFF
STUFF
STUFF
end of class
[System]
public class2
STUFF
STUFF
STUFF
STUFF
end of class
how would i go about "grabbing" the text that lies between(and including) the diff class... like between public class 2 and end of class?
Capture text between 2 lines(words)
Started by Army_Strong, Jun 14 2010 08:56 AM
8 replies to this topic
#1
Posted 14 June 2010 - 08:56 AM
|
|
|
#2
Posted 14 June 2010 - 12:00 PM
using System.Text.RegularExpressions; //or Regex, I am not sure
//...
Regex myReg = new Regex("public class 2 (.*?) end of class", RegexOptions.IgnoreCase | RegexOptions.Multline);
Match x = myReg.Match(//Insert here the content of the text file);
string whatYouWant = x.ToString();
This is the best way you can do it. Yet, it might not succeed depending on what you use instead of "public class2" and "end of class". Regular Expressions are pretty hard to play with, yet very powerful.
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics
#3
Posted 15 June 2010 - 06:57 AM
It looks like to me that you want the stuff in between the 2 lines for a specific reason. Almost like you are creating a simple language of sorts in a text file. While, I do agree with Davide Reg Exps are a great way to go about matching strings..another way is feasible. If you wanted to do something with the data immediately like grab the data between the 2 lines and create an Object in C# from it...you could simply read it line by line and process it that way..for instance:
using (StreamReader readFile = new StreamReader(path))
{
string line;
while ((line = readFile.ReadLine()) != null)
{
if(startReading(line))
{
//read the line, create an object, do what you want with the data
}
}
}
.
.
.
private bool startReading(string line)
{
if(line.Contains("public class")
return true;
if(line.Contains("end class")
return false;
}
#4
Posted 15 June 2010 - 09:27 AM
using (StreamReader readFile = new StreamReader(path))This code is wrong. Correction:
using (StreamReader readFile = new File.OpenText(path))Also, strings will never be null:
while ((line = readFile.ReadLine()) != null)Correction:
while ((line = readFile.ReadLine()) != "")
//Or, even better
while (!readFile.EndOfStream) {
string Line = readFile.ReadLine();
}
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics
#5
Posted 15 June 2010 - 09:29 AM
private bool startReading(string line)
{
if(line.Contains("public class")
return true;
if(line.Contains("end class")
return false;
}
Also, Intellisense wil underline this method because not all code return something. Imagine if line is "hello", what would the method do?
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics
#6
Posted 15 June 2010 - 09:47 AM
true and it was orginally just a quick fix. You'll need to make some adjustments to get it to work the way you want.
Thanks for the help bro ;)
private bool isInClass;
.
.
.
private bool startReading(string line)
{
if(line.Contains("public class"))
isInClass = true;
if(line.Contains("end class"))
isInClass = false;
return isInClass;
}
Thanks for the help bro ;)
#7
Posted 15 June 2010 - 10:03 AM
Hmmm, I have not tried this code in VS, but I wonder if it would say "use of unassigned variable isInClass".
Compiler errors haunted me so much before that now I know them by heart :).
Edit: Sorry if I am annoying :D.
Compiler errors haunted me so much before that now I know them by heart :).
Edit: Sorry if I am annoying :D.
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics
#8
Posted 15 June 2010 - 10:13 AM
No youre fine. Its good to be helpful. You definitely have a good point. However, if you initialize the variable in the Constructor
You should be fine :)
class MyClass
{
private bool isInClass;
public MyClass()
{
isInClass = false;
}
.
.
.
You should be fine :)
#9
Posted 15 June 2010 - 11:17 AM
Also, this line
is fine if youre using System.IO at the top of the file.
the orginal line variable was a List object in my other code :). So yep it needs to be checked agianst the empty sting and not agianst a null.
using (StreamReader readFile = new StreamReader(path))
is fine if youre using System.IO at the top of the file.
using System.IO;
the orginal line variable was a List object in my other code :). So yep it needs to be checked agianst the empty sting and not agianst a null.


Sign In
Create Account

Back to top









