【问题标题】:String split on words and queued punctuation characters单词和排队标点字符的字符串拆分
【发布时间】:2013-12-25 23:21:22
【问题描述】:

这是我现在使用的模式:

string pattern = @"^(\s+|\d+|\w+|[^\d\s\w])+$";

Regex regex = new Regex(pattern);
if (regex.IsMatch(inputString))
{
      Match match = regex.Match(inputString);

      foreach (Capture capture in match.Groups[1].Captures)
      {
           if (!string.IsNullOrWhiteSpace(capture.Value))
               tmpList.Add(capture.Value);
      }
 }
 return tmpList.ToArray<string>();

通过这个,我检索了一个字符串数组,每个单词的项目和每个标点符号的项目。

我现在想要实现的是仅将排队的标点符号分组在一个项目中,即现在如果一个接一个地有三个点,我会在我的数组中得到三个项目,每个项目都包含一个点。最终我想要一个带有三个点的项目(或任何其他标点符号)。

【问题讨论】:

    标签: c# regex regex-group


    【解决方案1】:

    试试这个正则表达式:

    ^(\s+|\d+|\w+|[^\d\s\w]+)+$
    

    说明

    【讨论】:

    • 请问您是如何构建这个简洁的架构的?
    【解决方案2】:

    尝试以下模式。我添加了一个额外的+。让我知道你是否打算做其他事情。希望对您有所帮助。

    string pattern = @"^(\s+|\d+|\w+|[^\d\s\w]+)+$";
    

    对于 inputString = "abc;..cbe;aaa...kjaskjas" 我得到这个结果:

    abc
    ;..
    cbe
    ;
    aaa
    ...
    kjaskjas
    

    【讨论】:

      猜你喜欢
      • 2010-09-26
      • 1970-01-01
      • 2018-11-17
      • 1970-01-01
      • 1970-01-01
      • 2018-09-07
      • 2011-06-12
      相关资源
      最近更新 更多