Question

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);
            }
        }
Was it helpful?

Solution

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]);
        }
    }

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top