Click here to Skip to main content
15,891,762 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have to take some words from a file and put them in a new string. I am stuk when i have to make a new string with that "k" var.

I have to do a string with all "k" values.


Thanks

What I have tried:

class Program
   {


       static void Main()
       {
           string[] text1 = new string[] { }; //i tried with a string but... nothing
           string text = System.IO.File.ReadAllText(@"Dat is my file location");
           List<string> list = new List<string>();  ///not even with a list... now i try to put the "k" to an list and after it to convert to string.. but nothing too :(

           Regex pattern1 = new Regex("(?<=ECU_ADDRESS).*?(?=)", RegexOptions.RightToLeft);               //The start and stop of my file
           MatchCollection allMatches1 = pattern1.Matches(text);
           StringBuilder builder = new StringBuilder();
           foreach (var k in allMatches1)    //here is the "k" var
           {



               Console.WriteLine(k);     // here i want to see all my k
               list.Add(k.ToString());            //   here i tried to see what i
                                                     //   have in list but... nothing
               list.ForEach(Console.WriteLine);

               ////////////////////builder.Append(k).Append(", ");

           }
Posted
Updated 29-Aug-18 21:40pm
v2

For starters, k is not a "word" it's a regex Match class instance, so to get the content it matched, you need to use k.Value, not k
Second, you are trying to write the list inside the loop that builds it - you want that at the end.
Thirdly, you have a StringBuilder and you don't even use it!

This looks like you are just guessing, and hoping it will all work - and that isn;t a strategy that will work, not at all. Stop guess, and start thinking - plan what you are going to do, and then if you don't know how, start breaking that down into smaller steps.

Try this:
string text = System.IO.File.ReadAllText(@"Dat is my file location");
Regex pattern1 = new Regex("(?<=ECU_ADDRESS).*?(?=)", RegexOptions.RightToLeft);               //The start and stop of my file
MatchCollection allMatches = pattern1.Matches(text);
StringBuilder words = new StringBuilder();
foreach (Match match in allMatches)
    {
    words.AppendLine(match.Value);
    }
Console.Write(words.ToString());
But ... your regex looks wrong. It looks like the one I gave you yesterday, but hacked my somebody who doesn't understand regexes:
(?<=ID Config:\\s+)\\w+(?=\\s)
Your version doesn't have a terminating condition (the "(?=\\s)" in mine is a "stop at the first whitespace", yours is just empty, and the ".*?" part says "match absolutely nothing" because "*" means "zero or more repetitions of anything at all", and the "?" after it says "as few as possible" which will always means "none, please".

Stop, look at your data, and work out what exactly identifies the start and end of the text you want to capture. Then use the sample I gave you yesterday and a copy of Expresso to work out what you need: Expresso[^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 
Comments
LRazvan96 30-Aug-18 3:51am    
You have right. I thank you so much!
One thing... does not going with .Value . I had to change .ToString();

And yeah, i should put the "\\s" right there.
Sorry for my questions.
OriginalGriff 30-Aug-18 4:02am    
Yes it does work with .Value - the .Value property is a string already!

https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.capture.value?view=netframework-4.7.2#System_Text_RegularExpressions_Capture_Value

(It's inherited from Capture my the Match class)

Don't apologise for questions - just try to think before you leap into code. It really does work out quicker and better, honest! :laugh:
Richard Deeming 30-Aug-18 10:10am    
Probably because you're using var for the loop variable, instead of Match as OG did.

The MatchCollection class only implements the non-generic IEnumerable interface, not the generic IEnumerable<Match> interface. As a result, if you ask the compiler to "guess" the type of the loop variable by using var, it will choose Object, since that's all the information it has.

If you explicitly tell it which type to use, it will use that type. (If you tell it to use the wrong type, you will get an InvalidCastException at run-time.)
The Matches method doesn't return a collection of strings. It returns instead a collection of, well, Match objects.
You should access the properties of such objects in order to obtain the strings you need. See the sample code in the documentation[^].
 
Share this answer
 
You are cluttering your code with a lot of random variables that you never use. The problem is quite easy to resolve as @OriginalGriff explained to you yesterday in your question at https://www.codeproject.com/Questions/1258200/How-do-I-writte-a-code-in-Csharp-for-read-some-wor[^].

C#
private static Regex findID= new Regex(
      "(?<=ID Config:\\s+)\\w+(?=\\s)",
    RegexOptions.Multiline
    | RegexOptions.CultureInvariant
    | RegexOptions.Compiled
    );
...
    String InpuText = System.IO.File.ReadAllText(@"Dat is my file location");
    MatchCollection ms = findID.Matches(InputText);
    foreach (Match m in ms)
    {
        Console.WriteLine(m.Value);
    }       


Apologies to OG for'fixing' his horrible braces indentation. :)
 
Share this answer
 
Comments
OriginalGriff 30-Aug-18 4:03am    
"Apologies to OG for 'ruining' his correct braces indentation."
FTFY! :laugh:

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