在特定子字符串

时间:2017-05-12 16:46:29

标签: c# regex

我想在下面的字符串中捕获以"需要"开头的粗体值。单词,而其他字符串中的单词以" skip"开头。并且"忽略"必须被忽略。我尝试了模式

need.+?(:"(?'index'\w+)"[,}])

但它只找到了第一个(ephasised)值。我如何才能使用RegEx获得所需的结果?

"跳过" :{" A":" ABCD123"," B":" ABCD1234"," C":&#34 ; ABCD1235"}

"需要" :{" A":" ZABCD123 "," B":" ZABCD1234 "" C":"的 ZABCD1235 "}

"忽略" :{" A":" SABCD123"," B":" SABCD1234"," C":&#34 ; SABCD1235"}

3 个答案:

答案 0 :(得分:1)

如果字段数已修复 - 您可以将其编码为:

^"need"\s*:\s*{"A":"(\w+)","B":"(\w+)","C":"(\w+)"}

Demo

如果标签在值之后 - 就像:

  

{" A":" ABCD123"" B":" ABCD1234"" C":& #34; ABCD1235"}:"跳过"   {" A":" ZABCD123"" B":" ZABCD1234"" C":" ZABCD1235"}:"需要"   {" A":" SABCD123"" B":" SABCD1234"" C":" SABCD1235"}:"忽略"

然后你可以使用无限正向前看

"\w+?":"(\w+?)"(?=.*"need")

Demo

在PCRE中无限积极地看待 are prohibited。 (禁止使用*+运算符查看后面的语法)。所以在你的情况下不是很有用

答案 1 :(得分:1)

我们将找到need并将我们找到的内容分组到Named Match Group => Captures。将有两个组,一个名为Index,其中包含A | B | C,另一个名为Data

匹配将保留我们的数据,如下所示:

enter image description here

从那里我们将他们加入一本词典:

enter image description here

以下是执行该魔术的代码:

string data =
@"""skip"" : {""A"":""ABCD123"",""B"":""ABCD1234"",""C"":""ABCD1235""}
""need"" : {""A"":""ZABCD123"",""B"":""ZABCD1234"",""C"":""ZABCD1235""}
""ignore"" : {""A"":""SABCD123"",""B"":""SABCD1234"",""C"":""SABCD1235""}";

string pattern = @"
\x22need\x22\s *:\s *{   # Find need
(                        # Beginning of Captures
   \x22                     #  Quote is \x22
   (?<Index>[^\x22] +)      # A into index.
   \x22\:\x22               # ':'
   (?<Data>[^\x22] +)       # 'Z...' Data
   \x22,?                   # ',(maybe)
)+                       # End of 1 to many Captures";


var mt = Regex.Match(data, 
                     pattern, 
                     RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture);

// Get the data capture into a List<string>.
var captureData = mt.Groups["Data"].Captures.OfType<Capture>()
                                            .Select(c => c.Value).ToList();

// Join the index capture data and project it into a dictionary.
var asDictionary = mt.Groups["Index"]
                     .Captures.OfType<Capture>()
                     .Select((cp, iIndex) => new KeyValuePair<string,string>
                                                 (cp.Value, captureData[iIndex]) )
                     .ToDictionary(kvp => kvp.Key, kvp => kvp.Value );

答案 2 :(得分:0)

You can't capture a dynamically set number of groups,所以我会运行类似这个正则表达式的东西

"need".*{.*,?".*?":(".+?").*}

[Demo]

使用'match_all'功能,或使用Agnius的建议