如何保持Regex.Split的分隔符?

时间:2021-07-22 21:46:40

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.

我想使用Regex类中的split函数来分割一个字符串。问题是它去掉了分隔符,我想保留它们。最好作为分割件中的独立元素。

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

根据我发现的其他讨论,只有不方便的方法可以实现这一点。

Any suggestions?

有什么建议吗?

4 个解决方案

#1


59  

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)
  • JavaScript:“123.456.789”.split(/(\)/ g)
  • Python: re.split(r"(\.)", "123.456.789")
  • Python:re.split(r”(\)。”、“123.456.789”)
  • Perl: split(/(\.)/g, "123.456.789")
  • Perl:分裂(/(\)/ g,“123.456.789”)

(Not Java though)

(而不是Java)

#2


6  

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

使用match查找字符串中的分隔符,然后获取值和分隔符。

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

#3


3  

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

假设输入是“abc1defg2hi3jkl”,regex将挑选数字。

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

部件将是:abc 1 defg2 hi 3 jkl

#4


0  

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] = ",";
    }

#1


59  

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)
  • JavaScript:“123.456.789”.split(/(\)/ g)
  • Python: re.split(r"(\.)", "123.456.789")
  • Python:re.split(r”(\)。”、“123.456.789”)
  • Perl: split(/(\.)/g, "123.456.789")
  • Perl:分裂(/(\)/ g,“123.456.789”)

(Not Java though)

(而不是Java)

#2


6  

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

使用match查找字符串中的分隔符,然后获取值和分隔符。

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

#3


3  

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

假设输入是“abc1defg2hi3jkl”,regex将挑选数字。

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

部件将是:abc 1 defg2 hi 3 jkl

#4


0  

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] = ",";
    }