3

hi and thanks for Reading . I have problems with Foreach loops.

from what I can see that foreach output is done one time for all phases.

here is an example of my problem.

string[] substrings = Regex.Split(Input, splitPattern);
foreach (string match in substrings)
{
    Console.WriteLine("'{0}'" , match + "this is first match ") ;
}

output is :

match 1 
match 2 
match 3 
this is first match

Questions 1 my problem is that the word " this is first match " display after all matchs, not after each match. I would like to maniplate each match alone. Because rather the writeline, I am going to assign the values of each match to an object, so if I have 5 matchs that mean 5 objects.

Question 2 : How to treat each match separated (with or without foreach loop).

thanks

andreasnico
  • 1,478
  • 14
  • 23
abdo refky
  • 145
  • 1
  • 11
  • 5
    Chances are your split pattern is incorrect and you only get one match that spans three lines (match 1-3 plus the line breaks). Please post your input data as well as the split pattern. – Christoph Feb 26 '16 at 07:08
  • Pattern : is xxx . input data : xxx abcdef abcdef xxx zxcvbn zxcvbn xxx poiuy poiuy . – abdo refky Feb 26 '16 at 07:35
  • those should be splitted to 3 sub strings . – abdo refky Feb 26 '16 at 07:36
  • if i know the answer of my second Question .i would then know if the is the problem with splitting or with foreach printing mechanism – abdo refky Feb 26 '16 at 07:40
  • Have a look at this [regex demo](http://regexstorm.net/tester?p=xxx&i=xxx+abcdef+abcdef+xxx+zxcvbn+zxcvbn+xxx+poiuy+poiuy). Does it return the parts you need? – Wiktor Stribiżew Feb 26 '16 at 07:52
  • regex demo giving me the parts i need . – abdo refky Feb 26 '16 at 08:05
  • but as a notice . result 0 is empty string .( not a problem for me ) and all my results are starting from 1 . (pattern working great ) – abdo refky Feb 26 '16 at 08:06

1 Answers1

0

If you need to use Regex.Split, you need to define a pattern (I see you are using a fake one xxx), then use the Regex.Split, and note that this function is likely to return empty values between consecutive delimiters and at the beginning of the string. This pruning can be done with some LINQ: .Where(p => !string.IsNullOrWhiteSpace(p)). This Where will iterate through all the elements in the IEnumarable collection, and check if the elements are null or just whitespace. If the element is null or whitespace only, it will be discarded from the result. .ToList() will convert the IEnumerable into a string list (List<string>).

Thus, after running Regex.Split, prune the result from the empty strings.

The @"" is a verbatim string literal. Inside it, all backslashes (and escape sequences) are treated as literals (@"\n" is a string containing 2 characters, \ and n, not a newline symbol). It is very convenient to write @"\s+" to match 1 or more whitespace than "\\s+".

Example:

using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

public class Test
{
    public static void Main()
    {
        var line = "xxx abcdef abcdef xxx zxcvbn zxcvbn xxx poiuy poiuy";
        var splts = Regex.Split(line, @"xxx").Where(p => !string.IsNullOrWhiteSpace(p)).ToList();
        //Console.WriteLine(splts.Count()); // => 3
        foreach (string match in splts)
        {
           Console.WriteLine("'{0}'" , match);
        }
}

Output:

' abcdef abcdef '
' zxcvbn zxcvbn '
' poiuy poiuy'
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Great example Wiktor . i added a text to the console.writeline . and it displayed with it's match output like i wanted to . but – abdo refky Feb 26 '16 at 08:56
  • if you may i wanna to what is meanning of .Where(p => !string.IsNullOrWhiteSpace(p)).ToList(); and why you used @ at the delimiter . i have done the same except those i just asked about . – abdo refky Feb 26 '16 at 09:07
  • I added more details to the answer. – Wiktor Stribiżew Feb 26 '16 at 09:12
  • Fantastic explanation . i am going to use all of your addtions . Question ((3)) ah BTW my Problem was the delimiter . i used something like " xxx : " and worked well when i removed the " : " . not sure why that . the " : " was working great with regex.matchs . – abdo refky Feb 26 '16 at 09:44
  • That is already something related to your input. I do not know what your input is and what real pattern you need. A tip: when you want to wite code in comments, use `\```\``. – Wiktor Stribiżew Feb 26 '16 at 09:46
  • i am using input similer to the one on this site . http://stackoverflow.com/questions/19193251/regex-to-get-the-words-after-matching-string . – abdo refky Feb 26 '16 at 09:52
  • If you want to get relevant help, please add a sample string to your question, and provide the expected result (what you need to match inside that string). – Wiktor Stribiżew Feb 26 '16 at 09:53
  • i am sorry to bother with all this questions , but 1 more thing :) what about my second main question . . – abdo refky Feb 26 '16 at 09:53
  • *How to treat each match separated (with or without foreach loop).* - What do you mean? Please provide an example input string and the expected result. – Wiktor Stribiżew Feb 26 '16 at 09:59
  • (1) if i want to print first match only or (2) adding first match only to an object of class . how to choose a match from other matchs . foreach make me do an action for all them once . :( – abdo refky Feb 26 '16 at 10:09
  • 1) the `var splts` contains *strings* from the split, the first one is accessed using `splts[0]`. 2) If you have defined some class, and it has a string property, you can initialize it with that value in `splts[0]`. `Regex.Matches` returns a Match collection. You can get hold of the match value using `match[0].Value` (if you got the matches as `var match = Regex.Matches(input, regex)`). – Wiktor Stribiżew Feb 26 '16 at 10:11