【问题标题】:How to really split string into string arrays without losing its part in C#?如何真正将字符串拆分为字符串数组而不丢失其在 C# 中的一部分?
【发布时间】:2013-05-29 18:38:43
【问题描述】:

我有什么

string ImageRegPattern = @"http://[\w\.\/]*\.jpg|http://[\w\.\/]*\.png|http://[\w\.\/]*\.gif";
string a ="http://www.dsa.com/asd/jpg/good.jpgThis is a good dayhttp://www.a.com/b.pngWe are the Best friendshttp://www.c.com";

我想要什么

string[] s;
s[0] = "http://www.dsa.com/asd/jpg/good.jpg";
s[1] = "This is a good day";
s[2] = "http://www.a.com/b.png";
s[3] = "We are the Best friendshttp://www.c.com";

邦斯:
如果 url 可以像下面那样拆分,那会更好,但如果不是,那也没关系。

s[3] = "We are the Best friends";
s[4] = "http://www.c.com";

问题是什么
我尝试使用下面的代码来拆分字符串,

string[] s= Regex.Split(sourceString, ImageRegPattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

但结果并不好,Split 方法似乎取出了所有匹配 ImageRegPattern 的字符串。但我希望他们留下来。我检查了 MSDN 上的 RegEx 页面,似乎没有合适的方法来满足我的需要。那么该怎么做呢?

【问题讨论】:

  • 我不认为有任何通用的解决方案来拆分该字符串(当然你可以制定一些方法来做到这一点,但它会非常具体)。你从 RegEx 中得不到任何回报,因为它在比赛中分裂。我个人会更改字符串的格式,除非有充分的理由不让您在字符串中添加分隔符。
  • 给定一个逗号分隔的列表,Regex.Split("1,2,3", ",") 将返回数组["1","2","3"]。您提供的模式定义了分隔符,而不是您想要保留的内容。 Regex.Split 不是你想在这里使用的。您试图保留文本 分隔符,这不是 Split 所做的。

标签: c# regex arrays string split


【解决方案1】:

您需要类似这种方法,它首先找到所有匹配项,然后将它们与它们之间的不匹配字符串一起收集到一个列表中。

更新:如果未找到匹配项,则添加条件处理。

private static IEnumerable<string> InclusiveSplit
(
    string source, 
    string pattern
)
{
  List<string> parts = new List<string>();
  int currIndex = 0;

  // First, find all the matches. These are your separators.
  MatchCollection matches = 
      Regex.Matches(source, pattern, 
      RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

  // If there are no matches, there's nothing to split, so just return a
  // collection with just the source string in it.
  if (matches.Count < 1)
  {
    parts.Add(source);
  }
  else
  {
    foreach (Match match in matches)
    {
      // If the match begins after our current index, we need to add the
      // portion of the source string between the last match and the 
      // current match.
      if (match.Index > currIndex)
      {
        parts.Add(source.Substring(currIndex, match.Index - currIndex));
      }

      // Add the matched value, of course, to make the split inclusive.
      parts.Add(match.Value);

      // Update the current index so we know if the next match has an
      // unmatched substring before it.
      currIndex = match.Index + match.Length;
    }

    // Finally, check is there is a bit of unmatched string at the end of the 
    // source string.
    if (currIndex < source.Length)
      parts.Add(source.Substring(currIndex));
  }

  return parts;
}

您的示例输入的输出将如下所示:

[0] "http://www.dsa.com/asd/jpg/good.jpg"
[1] "This is a good day"
[2] "http://www.a.com/b.png"
[3] "We are the Best friendshttp://www.c.com"

【讨论】:

    【解决方案2】:

    不要简单地低估的力量:

    (.*?)([A-Z][\w\s]+(?=http|$))

    说明:

    • (.*?) :分组并匹配所有内容,直到找到大写字母,在这个组中你会找到 url
    • ( : 开始组
      • [A-Z] : 匹配一个大写字母
      • [\w\s]+ : 匹配 a-z, A-Z, 0-9, _, \n, \r, \t, \f " " 任意字符1次以上
      • (?=http|$) :前瞻,检查后面是http还是行尾
      • ) :关闭组(在这里你会找到文本)

    Online demo

    注意:此解决方案是匹配字符串,而不是拆分它。

    【讨论】:

      【解决方案3】:

      我认为您需要一个多步骤的过程来插入一个分隔符,然后String.Split 命令可以使用该分隔符:

      resultString = Regex.Replace(rawString, @"(http://.*?/\w+\.(jpg|png|gif))", "|$1|", RegexOptions.IgnoreCase);
      if (a.StartsWith("|")
         a = a.Substring(1);
      string a = resultString.Split('|');
      

      【讨论】:

        【解决方案4】:

        这里明显的答案当然是不使用拆分,而是匹配图像模式并检索它们。话虽如此,使用拆分并非不可能。

        string ImageRegPattern = @"(?=(http://[\w./]*?\.jpg|http://[\w./]*?\.png|http://[\w./]*?\.gif))|(?<=(\.jpg|\.png|\.gif))"
        

        这将匹配字符串中后跟图像 url 或前面有 .jpg.gif.png 的点。

        我真的不建议这样做,我只是说你可以。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-04-02
          • 1970-01-01
          • 1970-01-01
          • 2011-10-13
          • 2021-07-06
          • 2012-06-27
          • 1970-01-01
          相关资源
          最近更新 更多