在为字符串定义拆分条件时,我似乎遇到一些问题。 该条件必须用于将字符串映射到字典。
中我有一个类似的问题,但是那里给出的解决方案可能适用于该特定情况,但不是一个可靠的解决方案。
不适用于这样的文本:
da:,en:H a full-bodied, vinous wine, which attracts wine connoisseurs with its well-balanced and lively bouquet. It combines crisp with a rich taste and long-lasting finish. Wines from one single vintage form the basis of this exceptional wine. som – sparkling since 1856,fr:,nl:,ru:
语言标签中的文本带有逗号,因此以后不能选择:
。
任何有关更可靠解决方案的建议。 我的意图是映射字符串,以便我可以获取信息,我正在寻找给定的语言标签。
答案 0 :(得分:1)
我建议使用正则表达式;提供
ru:
,en:
,
是分隔符:en: bla-bla-bla,ru: bla-bla-bla
你可以放
using System.Text.RegularExpressions;
...
string source = @"da:,en: H a full - bodied, vinous wine, which attracts wine connoisseurs with its well-balanced and lively bouquet.It combines crisp with a rich taste and long-lasting finish.Wines from one single vintage form the basis of this exceptional wine. som – sparkling since 1856,fr:,nl:,ru:";
Dictionary<string, string> result = Regex
.Matches(source, @"(?<lang>[a-z]{2}:)(?<value>.*?)(?=\,[a-z]{2}:|$)")
.OfType<Match>()
.ToDictionary(match => match.Groups["lang"].Value.TrimEnd(':'),
match => match.Groups["value"].Value);
Console.WriteLine(string.Join(Environment.NewLine, result
.Select(pair => $"language: {pair.Key}; text: {pair.Value}")));
结果:
language: da; text:
language: en; text: H a full - bodied, vinous wine, which attracts wine connoisseurs with its well-balanced and lively bouquet.It combines crisp with a rich taste and long-lasting finish.Wines from one single vintage form the basis of this exceptional wine. som – sparkling since 1856
language: fr; text:
language: nl; text:
language: ru; text: