-1

I have a text like this

#-cmd1-# Hakona Matata #-cmd2-#

I want to get all values that is like this #-TEXT-#

Here's the code I use, but it works only when there's one occurrence

var text = "#-adsfree-# hakona matata #-adsbottom-#";

Regex regex = new Regex("#-(.*)-#");
var v = regex.Match(text);
string s = v.Groups[1].ToString();
kakarot
  • 1
  • 4
  • You are only doing a single match. Use [Matches](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.matches). – wp78de Jul 18 '19 at 22:27
  • The only answer is to exclude the pound sign. `\#-([^#]*)-\#` –  Jul 18 '19 at 22:43

1 Answers1

1

I'm guessing that you might be designing an expression, maybe similar to:

(?<=#-)\b(\w+)\b(?=-#)

Test

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"(?<=#-)\b(\w+)\b(?=-#)";
        string input = @"#-adsfree-# hakona matata #-adsbottom-#";
        RegexOptions options = RegexOptions.Multiline;

        foreach (Match m in Regex.Matches(input, pattern, options))
        {
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
        }
    }
}

The expression is explained on the top right panel of this demo if you wish to explore/simplify/modify it.

Emma
  • 27,428
  • 11
  • 44
  • 69