0

I'd like to use Regex.Split to split on a delimiter, and also have that delimiter split onto all substrings.

For Example: I have a string "holidays/*/2024", and separator '*' and I want to have following: "holidays/*","*/2024"

This thread shows how to split and keep the separators as their own elements of the result. I have also been able to use Lookbehind (?<=[*]) and Lookahead (?=[*]) separately, each achieving half of the desired result. I am struggling to see how to use both (or alternative solutions) to accomplish the desired result in a single expression.

Any suggestions?

InSync
  • 4,851
  • 4
  • 8
  • 30
  • 1
    Does this answer your question? [Split a string with delimiters but keep the delimiters in the result in C#](https://stackoverflow.com/questions/4680128/split-a-string-with-delimiters-but-keep-the-delimiters-in-the-result-in-c-sharp) – gunr2171 May 25 '23 at 17:12
  • 1
    @gunr2171 The thread you linked shows how to add delimiters as separate substrings within the result set. I am hoping to have the delimiters be attached at the end and beginning of all substrings resulting from a split. – Andrew Weathers May 25 '23 at 17:16
  • 1
    Regex does not work like this, you can have overlapping matches, but it cannot go forward and then backward, it always proceeds from left to right. – Wiktor Stribiżew May 25 '23 at 17:22
  • 1
    Try : string[] result = rgx.Split(input); string newStr = string.Join("*"",""*", result); – jdweng May 25 '23 at 17:29
  • 1
    Not sure if you have to use split, but you might use 2 capture groups, and then check if the group values exist `([^/]+/\*(?=/))|(?<=(\*/[^/]+(?=/|$)))` https://regex101.com/r/IBAq7S/1\ – The fourth bird May 25 '23 at 17:43
  • 1
    Are there multiple delimiters? – InSync May 25 '23 at 17:47

3 Answers3

1

To split a string using Regex.

    using System;
    using System.Text.RegularExpressions;
    
    public class Program
    {
        public static void Main()
        {
            string input = "holidays/*/2024";
            string pattern = @"(?<=\*)|\*(?=/)";
    
            string[] substrings = Regex.Split(input, pattern);
            foreach (string substring in substrings)
            {
                Console.WriteLine(substring);
            }
        }
    }

    Output

    holidays/
    /2024

I hope this helps!

Dhiren Patel
  • 630
  • 8
  • 16
  • 1
    Unfortunately, it is imperative for my use-case that the delimiter persist on both resulting substrings. Expected result: [ holidays/* , */2024 ]. – Andrew Weathers May 25 '23 at 17:28
0

I don't know C#, but a simple loop should be enough:

string[] splitted = text.Split(delimiter);

for (var i = 0; i < splitted.Length; i++) {
    var fragment = splitted[i];

    if (i > 0) {
        fragment = delimiter + fragment;
    }

    if (i < splitted.Length - 1) {
        fragment += delimiter;
    }

    Console.WriteLine(fragment);
}

Try it on dotnetfiddle.net.

InSync
  • 4,851
  • 4
  • 8
  • 30
0

Another solution is to use this horrible-looking regex:

Regex re = new Regex(
  @"
    (?:^|\*)        # Match and capture either the beginning of string or '*',
    (?=             # then
      ([^*]+)       # a group 1+ non-'*' characters, followed by
      (\*|$)        # another group of '*' or the end of string.
    )
  ",
  RegexOptions.IgnorePatternWhitespace
);

...then .Join() the groups in each match to get the final result:

MatchCollection matches = re.Matches(text);

foreach (Match match in matches) {
  Console.WriteLine(
    String.Join("", (
      from Group matchGroup in match.Groups
      where matchGroup.ToString() != ""
      select matchGroup.ToString()
    ))
  );
}

Try it on dotnetfiddle.net.

InSync
  • 4,851
  • 4
  • 8
  • 30