Click here to Skip to main content
15,917,645 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Hello,
I have to writte a sequence in C#. Dat have to read from a file (does not matter what kind of file), just the words between some ... words.
EX: "ID Config: 1423AAA end config
ID Config: 242gggg end config"
---------
And i have to get just dat "1423AAA", from each line.

The results must be put in one string (I think a loooong string).

Please help me :(

Sry for my english.

What I have tried:

Start with StreamReader sr = new StreamReader(file);
....

and i can't find the condition :(
Posted
Updated 29-Aug-18 1:23am

You should really be able to find the answer on the Internet, eg,:

c# - Reading a text file word by word - Stack Overflow[^]
 
Share this answer
 
Comments
LRazvan96 29-Aug-18 5:57am    
But dat numbers will change every time in the file.
It really depends on how consistent the data is, and if it's repeated.
If your data is always of the form
... ID Config: nnnnaaa end config ...
Then I'd use a regex:
(?<=ID Config:\s+)\w+(?=\s)
Would do it:
C#
private static Regex findID= new Regex(
      "(?<=ID Config:\\s+)\\w+(?=\\s)",
    RegexOptions.Multiline
    | RegexOptions.CultureInvariant
    | RegexOptions.Compiled
    );
...
    MatchCollection ms = findID.Matches(InputText);
    foreach (Match m in ms)
        {
        Console.WriteLine(m.Value);
        }
 
Share this answer
 
Comments
LRazvan96 29-Aug-18 6:07am    
There will be more datas in dat document. Like...

"blabal
ballba bla ba nevermind
ID COnfig: 1243FS end config;
again words..
and again
ID Config: 1241gf end config;
.. "
OriginalGriff 29-Aug-18 6:10am    
That's why my "of the form" sample had ellipses ...

Try the Regex code and see what happens!
LRazvan96 29-Aug-18 6:27am    
Sry, but i can't figured out without the form :( i am confuzed now , because i never used regex sintax before.

It have to start reading from my file, to take the all strings and put in a string to use it in rest of my program.
Sry for this :(
OriginalGriff 29-Aug-18 6:35am    
Oh come on!
You know how to read all the text from a file, yes?
LRazvan96 29-Aug-18 6:43am    
I am beginner in this, but nevermind.
It can be easy but i can t figured out.
And i don t know if i have to use File.ReadAllText or a simple instruction.

If it can help...
static void Main(string[] args)
{
StreamReader fin = new StreamReader("D:\\path..);
string line = "";
while ((line = fin.ReadLine()) != null)
{
// and here i don t know
}

If you are a beginner and you are already confused just by using a tool like regex you might want to consider alternative formats like XML. Search for articles on XmlSerializer or DataContractSerializer

- its really easy to read and write Xml in .Net if you take the time to evaluate the option ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900