Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep the delimiters of Regex.Split?

Tags:

c#

regex

split

People also ask

Can we use regex in Split?

You do not only have to use literal strings for splitting strings into an array with the split method. You can use regex as breakpoints that match more characters for splitting a string.

How do you split with delimiter?

You can use the split() method of String class from JDK to split a String based on a delimiter e.g. splitting a comma-separated String on a comma, breaking a pipe-delimited String on a pipe, or splitting a pipe-delimited String on a pipe.


Just put the pattern into a capture-group, and the matches will also be included in the result.

string[] result = Regex.Split("123.456.789", @"(\.)");

Result:

{ "123", ".", "456", ".", "789" }

This also works for many other languages:

  • JavaScript: "123.456.789".split(/(\.)/g)
  • Python: re.split(r"(\.)", "123.456.789")
  • Perl: split(/(\.)/g, "123.456.789")

(Not Java though)


Use Matches to find the separators in the string, then get the values and the separators.

Example:

string input = "asdf,asdf;asdf.asdf,asdf,asdf";

var values = new List<string>();
int pos = 0;
foreach (Match m in Regex.Matches(input, "[,.;]")) {
  values.Add(input.Substring(pos, m.Index - pos));
  values.Add(m.Value);
  pos = m.Index + m.Length;
}
values.Add(input.Substring(pos));

Say that input is "abc1defg2hi3jkl" and regex is to pick out digits.

String input = "abc1defg2hi3jkl";
var parts = Regex.Matches(input, @"\d+|\D+")
            .Cast<Match>()
            .Select(m => m.Value)
            .ToList();

Parts would be: abc 1 defg 2 hi 3 jkl


For Java:

Arrays.stream("123.456.789".split("(?<=\\.)|(?=\\.)+"))
                .forEach((p) -> {
                    System.out.println(p);
                });

outputs:

123
.
456
.
789

inspired from this post (How to split string but keep delimiters in java?)