EDN Admin
Well-known member
Hello all. I need help with an issue and i cant think of a good way to do this. I need some community help. I have a program that is going to extract specific information out of a text file generated from a pdf. The text file stores one word per line. These
are not very large files, memory efficiency is not the highest priority. But I need to be able to search for a specific match in my text file, and pull the next line, the next two lines, and is some cases, skip a line and pull the following line after the
match is found. I am fairly new at this so any advice would be greatly appreciated...
heres what i have so far:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.ComponentModel;
<span style="color:Blue; using System.Data;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; using System.Diagnostics;
<span style="color:Blue; using System.IO;
<span style="color:Blue; using System.Text.RegularExpressions;
<span style="color:Blue; namespace ISIDE
{
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class Form1 : Form
{
<span style="color:Blue; public Form1()
{
InitializeComponent();
}
<span style="color:Blue; private <span style="color:Blue; void button1_Click(<span style="color:Blue; object sender, EventArgs e)
{
<span style="color:Green; //form2 field variables
<span style="color:Blue; string LName = <span style="color:#A31515; "";
<span style="color:Blue; string FName = <span style="color:#A31515; "";
<span style="color:Blue; string DocName = <span style="color:#A31515; "";
<span style="color:Blue; string Date = <span style="color:#A31515; "";
<span style="color:Green; //check to see if file exsists
<span style="color:Blue; if (File.Exists(<span style="color:#A31515; "fred.pdf"))
{
<span style="color:Green; //runs file thru the PTMCD app to return a text file containing data
Process p = <span style="color:Blue; new Process();
p.StartInfo.FileName = System.IO.Path.Combine(Application.StartupPath, <span style="color:#A31515; "PTCMD.exe");
p.StartInfo.Arguments = <span style="color:#A31515; "fred.pdf temp.txt";
p.Start();
p.WaitForExit();
<span style="color:Blue; string filePath = <span style="color:#A31515; @"c:temptest.txt";
<span style="color:Blue; int LineNmbr = 0;
<span style="color:Green; //final report reg set
Regex Patient = <span style="color:Blue; new Regex(<span style="color:#A31515; "Patient:");
Regex Order = <span style="color:Blue; new Regex(<span style="color:#A31515; "Order");
Regex Ord = <span style="color:Blue; new Regex(<span style="color:#A31515; "Ord");
<span style="color:Green; //cumulative report reg set
Regex Cum = <span style="color:Blue; new Regex(<span style="color:#A31515; "Cumulative");
Regex Name = <span style="color:Blue; new Regex(<span style="color:#A31515; "Name:");
Regex Admit = <span style="color:Blue; new Regex(<span style="color:#A31515; "Admit");
Regex Admitted = <span style="color:Blue; new Regex(<span style="color:#A31515; "Admitted:");
StreamReader objReader;
<span style="color:Blue; using (StreamReader r = <span style="color:Blue; new StreamReader(<span style="color:#A31515; "temp.txt"))
{
<span style="color:Blue; string line;
objReader = <span style="color:Blue; new StreamReader(filePath);
<span style="color:Blue; while ((line = r.ReadLine()) != <span style="color:Blue; null)
{
LineNmbr++;
<span style="color:Green; // Try to match each line against the Regex.
Match a = Patient.Match(line);
Match b = Order.Match(line);
Match c = Ord.Match(line);
Match d = Name.Match(line);
Match h = Admit.Match(line);
Match f = Admitted.Match(line);
Match g = Cum.Match(line);
<span style="color:Green; //get info from cululative trend report
<span style="color:Blue; if (g.Success)
{
<span style="color:Green; //gets patients last , first name
<span style="color:Blue; if (d.Success)
{
<span style="color:Blue; int NameCounter = LineNmbr + 1;
<span style="color:Green; //first need to goto the next line
<span style="color:Green; //reads line and assigns it as variable
LName = objReader.ReadLine();
<span style="color:Green; //then i can go to the next line...
<span style="color:Green; //reads line and assigns it as variable
FName = objReader.ReadLine();
}
<span style="color:Blue; if (f.Success)
{
<span style="color:Green; //first need to go to next line
<span style="color:Green; //read line and assign it as date
Date = objReader.ReadLine();
}
<span style="color:Blue; if (h.Success)
{
<span style="color:Green; //first go to next-next line
<span style="color:Green; //read line and assign it as dr
DocName = objReader.ReadLine();
}
}
<span style="color:Green; //gets info from any final report
<span style="color:Blue; else
<span style="color:Blue; if (a.Success)
{
<span style="color:Green; //first need to goto the next line
<span style="color:Green; //reads line and assigns it as variable
LName = objReader.ReadLine();
<span style="color:Green; //then i can go to the next line...
<span style="color:Green; //reads line and assigns it as variable
FName = objReader.ReadLine();
}
<span style="color:Blue; if (b.Success)
{
<span style="color:Green; //first need to go to next line
<span style="color:Green; //read line and assign it as date
Date = objReader.ReadLine();
}
<span style="color:Blue; if (c.Success)
{
<span style="color:Green; //first go to next-next line
<span style="color:Green; //read line and assign it as dr
DocName = objReader.ReadLine();
}
}
}
<span style="color:Green; //format pulled text
LName.ToLower();
LName.TrimEnd(<span style="color:#A31515; ,);
FName.ToLower();
DocName.ToLower();
DocName.TrimEnd(<span style="color:#A31515; ,);
<span style="color:Green; //close objReader
objReader.Close();
}
<span style="color:Blue; else
MessageBox.Show(Properties.Resources.PdfError, <span style="color:#A31515; "PDF Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
[/code]
<br/>
<br/>
View the full article
are not very large files, memory efficiency is not the highest priority. But I need to be able to search for a specific match in my text file, and pull the next line, the next two lines, and is some cases, skip a line and pull the following line after the
match is found. I am fairly new at this so any advice would be greatly appreciated...
heres what i have so far:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.ComponentModel;
<span style="color:Blue; using System.Data;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; using System.Diagnostics;
<span style="color:Blue; using System.IO;
<span style="color:Blue; using System.Text.RegularExpressions;
<span style="color:Blue; namespace ISIDE
{
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class Form1 : Form
{
<span style="color:Blue; public Form1()
{
InitializeComponent();
}
<span style="color:Blue; private <span style="color:Blue; void button1_Click(<span style="color:Blue; object sender, EventArgs e)
{
<span style="color:Green; //form2 field variables
<span style="color:Blue; string LName = <span style="color:#A31515; "";
<span style="color:Blue; string FName = <span style="color:#A31515; "";
<span style="color:Blue; string DocName = <span style="color:#A31515; "";
<span style="color:Blue; string Date = <span style="color:#A31515; "";
<span style="color:Green; //check to see if file exsists
<span style="color:Blue; if (File.Exists(<span style="color:#A31515; "fred.pdf"))
{
<span style="color:Green; //runs file thru the PTMCD app to return a text file containing data
Process p = <span style="color:Blue; new Process();
p.StartInfo.FileName = System.IO.Path.Combine(Application.StartupPath, <span style="color:#A31515; "PTCMD.exe");
p.StartInfo.Arguments = <span style="color:#A31515; "fred.pdf temp.txt";
p.Start();
p.WaitForExit();
<span style="color:Blue; string filePath = <span style="color:#A31515; @"c:temptest.txt";
<span style="color:Blue; int LineNmbr = 0;
<span style="color:Green; //final report reg set
Regex Patient = <span style="color:Blue; new Regex(<span style="color:#A31515; "Patient:");
Regex Order = <span style="color:Blue; new Regex(<span style="color:#A31515; "Order");
Regex Ord = <span style="color:Blue; new Regex(<span style="color:#A31515; "Ord");
<span style="color:Green; //cumulative report reg set
Regex Cum = <span style="color:Blue; new Regex(<span style="color:#A31515; "Cumulative");
Regex Name = <span style="color:Blue; new Regex(<span style="color:#A31515; "Name:");
Regex Admit = <span style="color:Blue; new Regex(<span style="color:#A31515; "Admit");
Regex Admitted = <span style="color:Blue; new Regex(<span style="color:#A31515; "Admitted:");
StreamReader objReader;
<span style="color:Blue; using (StreamReader r = <span style="color:Blue; new StreamReader(<span style="color:#A31515; "temp.txt"))
{
<span style="color:Blue; string line;
objReader = <span style="color:Blue; new StreamReader(filePath);
<span style="color:Blue; while ((line = r.ReadLine()) != <span style="color:Blue; null)
{
LineNmbr++;
<span style="color:Green; // Try to match each line against the Regex.
Match a = Patient.Match(line);
Match b = Order.Match(line);
Match c = Ord.Match(line);
Match d = Name.Match(line);
Match h = Admit.Match(line);
Match f = Admitted.Match(line);
Match g = Cum.Match(line);
<span style="color:Green; //get info from cululative trend report
<span style="color:Blue; if (g.Success)
{
<span style="color:Green; //gets patients last , first name
<span style="color:Blue; if (d.Success)
{
<span style="color:Blue; int NameCounter = LineNmbr + 1;
<span style="color:Green; //first need to goto the next line
<span style="color:Green; //reads line and assigns it as variable
LName = objReader.ReadLine();
<span style="color:Green; //then i can go to the next line...
<span style="color:Green; //reads line and assigns it as variable
FName = objReader.ReadLine();
}
<span style="color:Blue; if (f.Success)
{
<span style="color:Green; //first need to go to next line
<span style="color:Green; //read line and assign it as date
Date = objReader.ReadLine();
}
<span style="color:Blue; if (h.Success)
{
<span style="color:Green; //first go to next-next line
<span style="color:Green; //read line and assign it as dr
DocName = objReader.ReadLine();
}
}
<span style="color:Green; //gets info from any final report
<span style="color:Blue; else
<span style="color:Blue; if (a.Success)
{
<span style="color:Green; //first need to goto the next line
<span style="color:Green; //reads line and assigns it as variable
LName = objReader.ReadLine();
<span style="color:Green; //then i can go to the next line...
<span style="color:Green; //reads line and assigns it as variable
FName = objReader.ReadLine();
}
<span style="color:Blue; if (b.Success)
{
<span style="color:Green; //first need to go to next line
<span style="color:Green; //read line and assign it as date
Date = objReader.ReadLine();
}
<span style="color:Blue; if (c.Success)
{
<span style="color:Green; //first go to next-next line
<span style="color:Green; //read line and assign it as dr
DocName = objReader.ReadLine();
}
}
}
<span style="color:Green; //format pulled text
LName.ToLower();
LName.TrimEnd(<span style="color:#A31515; ,);
FName.ToLower();
DocName.ToLower();
DocName.TrimEnd(<span style="color:#A31515; ,);
<span style="color:Green; //close objReader
objReader.Close();
}
<span style="color:Blue; else
MessageBox.Show(Properties.Resources.PdfError, <span style="color:#A31515; "PDF Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
[/code]
<br/>
<br/>
View the full article