سؤال

I cant find regex expression of <#anystring#>?

Ex: <#sda#> or <#32dwdwwd#> or any of string between <# and #>

I tried "<#[^<#]+#>" but this has outputted only the first occurrence.

        string sample = "\n\n<#sample01#> jus some words <#sample02#> <#sample03#> just some words ";
        Match match = Regex.Match(sample, "<#[^<#]+#>");
        if (match.Success)
        {
            foreach (Capture capture in match.Captures)
            {
                Console.WriteLine(capture.Value);
            }
        }
هل كانت مفيدة؟

المحلول

You are using the match() method. Try reading the documentation and you will see, that it returns only the first match.

Try the matches() method instead, it returns a MatchCollection.

It would look something like this (careful, not tested written directly here)

string sample = "\n\n<#sample01#> jus some words <#sample02#> <#sample03#> just some words ";
    MatchCollection mc = Regex.Matches(sample, "<#(.*?)#>");
    foreach (Match m in mc)
        {
            Console.WriteLine(m.Groups[0]);
        }
    }

نصائح أخرى

try this one , it should work

UPDATED

<#(.*?)#>

  • The dot is any character except new line (\n).
  • The * means 0 or more.
  • The ? is used to make it ungreedy.

source here

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top