0

Here is the link for the my earlier post Regex with multiple repetation

I am trying to use the same regex pattern

Regex regex_pname = new Regex(@"--(?<PName>8W1_.*?)--.*?(?<MType>PTP|LIN).*?(?<PointNa>" +list[i]+")", RegexOptions.Singleline);

Where List[i] contain a array of string

List[0]=SG006565
List[1]=SG0004254
Linst[2]=SG0004249

for inserting a for loop I would like to find the correct Matching Group (PName) which contain string like 8W1_805_431____0210_A01_0001_L

Here is link of the input string http://freetexthost.com/o6yb1gp0oh

Here is my piece of code

for (int i = 0; i < list.Count; i++) // Loop with for.
{
    Regex regex_pname = new Regex(@"--(?<PName>8W1_.*?)--.*?(?<MType>PTP|LIN).*?(?<PointNa>" +list[i]+")", RegexOptions.Singleline);
    Console.Write("Regex \n"+ regex_pname + "\n ");

    foreach (Match Match21 in regex_pname.Matches(text))
    {
        GroupCollection groups = Match21.Groups;
        //Console.Write("Match 21 \n" + Match21 + "\n ");
        string v = groups["PName"].ToString();

        //Console.WriteLine("PName \n" + v + "\n PName \n");
        listView1.Items.Add(v);
    }
}

When i try to run this code it always gives me first match

(8W1_803_015____0210_A01_0001_R)

three times.

Community
  • 1
  • 1
Tarun. P
  • 422
  • 7
  • 16
  • What is the boundary between the records? `;FOLD` and `;ENDFOLD`? The `.*?` does not match the shortest window between tokens in a regex pattern. It matches the first `--8W1` and then up to the value you have on the right. – Wiktor Stribiżew Feb 02 '16 at 12:27
  • @WiktorStribiżew each line starts with ;FOLD and end with ;Endfold. I problem is to identify (8W1_803_015____0210_A01_0001_R) unitl it find a line with (PTP | LIN) with point name SG006565. If pattern is not able to find this then I have to write NONE. – Tarun. P Feb 03 '16 at 05:06
  • So, I guess the expected results for the above is 1) - a match is found, 2) match is not found and 3) match is found. Try [`--(?8W1_[^-]*(?:-(?!-)[^-]*)*)--(?:(?!\b(?:PTP|LIN)\b).)*\b(?PTP|LIN)\b(?:(?!\b(?:PTP|LIN)\b).)*?(?" + Regex.Escape(list[i]) + ")`](https://regex101.com/r/iL9gS5/1) – Wiktor Stribiżew Feb 03 '16 at 07:48
  • If that does not help, please think about what SHOULD NOT BE present between `LIN`/`PTP` and the `PointNa`? – Wiktor Stribiżew Feb 03 '16 at 08:07

0 Answers0