user1675891
user1675891

Reputation:

How to keep the delimiters of Regex.Split?

I'd like to split a string using the Split function in the Regex class. The problem is that it removes the delimiters and I'd like to keep them. Preferably as separate elements in the splitee.

According to other discussions that I've found, there are only inconvenient ways to achieve that.

Any suggestions?

Upvotes: 39

Views: 39136

Answers (6)

H_MONK
H_MONK

Reputation: 1

for c#: Split paragraph to sentance keeping the delimiters sentance is splited by . or ? or ! followed by one space (otherwise if there any mail id in sentance it will be splitted)

string data="first. second! third? ";
Regex delimiter = new Regex("(?<=[.?!] )"); //there is a space between ] and )
string[] afterRegex=delimiter.Split(data);

Result

first. second! third?

Upvotes: 0

box110a
box110a

Reputation: 23

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?)

Upvotes: 1

Markus Jarderot
Markus Jarderot

Reputation: 89171

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)

Upvotes: 98

I4V
I4V

Reputation: 35353

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

Upvotes: 4

Guffa
Guffa

Reputation: 700152

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

Upvotes: 8

Michael Ross
Michael Ross

Reputation: 570

Add them back:

    string[] Parts = "A,B,C,D,E".Split(',');
    string[] Parts2 = new string[Parts.Length * 2 - 1];
    for (int i = 0; i < Parts.Length; i++)
    {
        Parts2[i * 2] = Parts[i];
        if (i < Parts.Length - 1)
            Parts2[i * 2 + 1] = ",";
    }

Upvotes: 0

Related Questions