0

I'm new to RegEx and I find it really confusing, so I would like a simple example of how to extract multiple strings of the same type.

Let's say we have a random string and I want to capture anything that is surrounded by "###". So I assume the pattern would look something like this:

string str = "dsad###hi###vdkv324da###dog###fs";
Regex pattern = new Regex("###.+###");

How do I match and store more than one occurrences? And the bigger question - after I do that I'll be left with "###hi###" and "###dog###", so how do I extract the "hi" and "dog" from those?

Anna
  • 35
  • 5
  • 3
    First of all, you must define your requirement. When you say _anything that is surrounded by "###"_ that's not unambiguous. For instance, in your sample string, the whole `hi###vdkv324da###dog` part lies between two `###`s. So what is your solution to that? Are you using two adjacent `###` an opening and closing parts? – Sach Aug 19 '17 at 16:03
  • There is a nice tool to test regular expressions: https://regex101.com/ Although, keep in mind that .NET C# regexes are a little specific, so some tokens may not work. – Martin Heralecký Aug 19 '17 at 16:06
  • 1
    Regex isn't always the best method to use. In this case a string() method will work : string[] split = str.Split(new string[] { "###" }, StringSplitOptions.RemoveEmptyEntries); – jdweng Aug 19 '17 at 16:21

2 Answers2

2

A working solution:

#{3}([^#]+)#{3}

#{3}      // 3 number signs, then
([^#]+)   // 1 or more characters excluding a number sign (captured as group), then
#{3}      // 3 number signs

Demo

linden2015
  • 887
  • 7
  • 9
1

Here's another solution, along with how to extract string parts. Uses a version of your own original Regex, and grabs the strings between adjacent ###.

string str = "dsad###hi###vdkv324da###dog###fs###d###";
Regex pattern = new Regex("###.+?###");

List<string> matched = new List<string>();
foreach(Match mat in pattern.Matches(str))
{
    string val = mat.Value;
    int start = val.IndexOf("###") + 3;
    int len = val.LastIndexOf("###") - start;
    matched.Add(val.Substring(start, len));
}

Update: More concise extraction of string:

foreach(Match mat in pattern.Matches(str))
{
    matched.Add(mat.Value.Trim('#'));
}
Sach
  • 10,091
  • 8
  • 47
  • 84