read a specific part of a text file into an ARRAY in C#
I have a text file including several strings and doubles.
I want to find a specific string in the text and read all double numbers after it which are located between two slashes"/".
The number of double data could be unknown-but we just know they are bounded with two slashes-
the file is two big and including several strings
for example my data file is like this and I want to put all Double numbers,bounded between slashes and after "POROSITY" into an array
with name "POROSITY".
**************************************************
Permeability
/
1 2 3 4
5 6 7 8
9 10
11 12 13 14
15 15 17 18
19 20
21 22 23 24
25 26 27 28
29 30
.
.
.
/
POROSITY
/
1 2 3 4
5 6 7 8
9 10
11 12 13 14
15 15 17 18
19 20
21 22 23 24
25 26 27 28
29 30
.
.
.
/
Kirtan PatelPosted Oct 11, 2009, 1:28 AM
Code Tested on Following Formatted Data In TextFile and Matched Success fully !!
----------------------------------------------------------------------
/Permeability / 1 2
3 4 5 6 7 8 9 10 11 12 13
14 15 15 17 18 19 20 21 22
23 24 25 26 27 28 29 30 / POROSITY
/
5
10 15
30 50 60
78 80 90 100
150 /
----------------------------------------------------------------------------
private void btnSearch_Click(object sender, EventArgs e)
{
try
{
string path = Environment.CurrentDirectory + @"\TextFile1.txt";
string TextData = File.ReadAllText(path);
int[] x = new int[100];
string CustomRegEx = txtSearchBox.Text.Trim() + "[/0-9 \n\r]*/";
Regex re = new Regex(CustomRegEx);
string FilteredText = TextData.Replace('\n', ' ');
MatchCollection mc = re.Matches(FilteredText);
string SearchedString = mc[0].ToString().Replace(txtSearchBox.Text.Trim(), string.Empty);
string ExtractedNumbers = SearchedString.Replace("/", String.Empty);
string FilteredCR = ExtractedNumbers.Replace('\r',' ');
string[] NumbersStrings = FilteredCR.Split(' ');
int i = 0;
listBox1.Items.Clear();
foreach (string str in NumbersStrings)
{
if (str != "")
{
int Number = int.Parse(str);
x[i] = Number;
i++;
listBox1.Items.Add(Number.ToString());
}
}
}
catch
{
MessageBox.Show("No match Found !!");
}
}
yamidPosted Oct 11, 2009, 8:21 AM
Roei BarPosted Oct 10, 2009, 5:47 PM
just replace the button event with this code:
yamidPosted Oct 10, 2009, 3:55 PM
Kirtan PatelPosted Oct 10, 2009, 3:30 PM
I Also Have included Project Files Take A Look At it
Download Project Files
private void btnSearch_Click(object sender, EventArgs e)
{
try
{
string path = Environment.CurrentDirectory + @"\TextFile1.txt";
string TextData = File.ReadAllText(path);
int[] x = new int[100];
//Below Code Will Change the Matching pattern of Regex on SearchText Box
//this Will Match One Portion of the String
string CustomRegEx = txtSearchBox.Text.Trim() + "[/0-9 ]*/";
Regex re = new Regex(CustomRegEx);
//Collecti Founded Matches in Match Collection
MatchCollection mc = re.Matches(TextData);
// Now our Matched String is mc[0] from String
//Here Value of mc[0] will be "POROSITY / 5 10 15 30 50 60 78 80 90 100 150 /"
//Now we Only Have to do Operation on this String mc[0]
//Below Replace will Remove the "POROSITY" Word From String
string SearchedString = mc[0].ToString().Replace(txtSearchBox.Text.Trim(), string.Empty);
//Now Our String is "/ 5 10 15 30 50 60 78 80 90 100 150 /"
//So We have to Remove '/' also so used Below Code
string ExtractedNumbers = SearchedString.Replace("/", String.Empty);
//Now our String After All Operation is : 5 10 15 30 50 60 78 80 90 100 150
//By Split Operation we Split Eeach Number as Word in Array "NumberString"
//It Will Be Like
//NumberStrins[0] = "5"
//NumberStrins[0] = "10" ... continue..
string[] NumbersStrings = ExtractedNumbers.Split(' ');
// now We have all the Numbers as String in NumberString Array
// Just Convert It To integer and Add It To Array By Below Code
int i = 0;
listBox1.Items.Clear();
foreach (string str in NumbersStrings)
{
if (str != "")
{
int Number = int.Parse(str);
x[i] = Number;
i++;
listBox1.Items.Add(Number.ToString());
}
}
}
catch
{
}
yamidPosted Oct 10, 2009, 2:58 PM
Kirtan PatelPosted Oct 10, 2009, 9:23 AM
Friend,
here i Coded what you need :)
dont forget to mark "Do you like this answer" please it will give me some credits :)
Tested On Following Data
---------------------------------------------------- ---------------------------------------------------------------------------
/Permeability / 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 15 17 18 19 20 21 22 23 24 25 26 27 28 29 30 / POROSITY / 5 10 15 30 50 60 78 80 90 100 150 /
----------------------------------------------------------------------------------------------------------------------------
using System.IO;
using System.Text.RegularExpressions;
private void btnSearch_Click(object sender, EventArgs e)
{
try
{
string path = Environment.CurrentDirectory + @"\TextFile1.txt";
string TextData = File.ReadAllText(path);
int[] x = new int[100];
string CustomRegEx = txtSearchBox.Text.Trim() + "[/0-9 ]*/";
Regex re = new Regex(CustomRegEx);
MatchCollection mc = re.Matches(TextData);
string SearchedString = mc[0].ToString().Replace(txtSearchBox.Text.Trim(), string.Empty);
string ExtractedNumbers = SearchedString.Replace("/", String.Empty);
//Device the String in Small Parts
string[] NumbersStrings = ExtractedNumbers.Split(' ');
int i = 0;
listBox1.Items.Clear();
foreach (string str in NumbersStrings)
{
if (str != "")
{
int Number = int.Parse(str);
x[i] = Number;
i++;
listBox1.Items.Add(Number.ToString());
}
}
}
catch
{
}
}