Jump to content

Capture text between 2 lines(words)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
Army_Strong

Army_Strong

    Newbie

  • Members
  • Pip
  • 1 posts
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?

#2
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
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
While(!EOF)

While(!EOF)

    Newbie

  • Members
  • PipPip
  • 21 posts
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
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
 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
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
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
While(!EOF)

While(!EOF)

    Newbie

  • Members
  • PipPip
  • 21 posts
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.

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
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
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.
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#8
While(!EOF)

While(!EOF)

    Newbie

  • Members
  • PipPip
  • 21 posts
No youre fine. Its good to be helpful. You definitely have a good point. However, if you initialize the variable in the Constructor


class MyClass
{
     private bool isInClass;

    public MyClass()
    {
             isInClass = false;
    }
  .
  .
  .

You should be fine :)

#9
While(!EOF)

While(!EOF)

    Newbie

  • Members
  • PipPip
  • 21 posts
Also, this line

  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.