C# 匹配重复模式
我当前正在尝试匹配并捕获以下输入中的文本:C# 匹配重复模式,c#,regex,C#,Regex,我当前正在尝试匹配并捕获以下输入中的文本: field: one two three field: "moo cow" field: +this 我可以将字段:与[a-z]*\:匹配,但是我似乎无法匹配其余的内容,到目前为止,我的尝试只能捕获我不想做的所有内容。不要忘记,您实际上可以匹配正则表达式中的文字字符串。如果您的模式是这样的: field\: 您将按字面匹配“field:”,而不匹配任何其他内容。如果您知道它总是按字面匹配field:则绝对不需要正则表达式: var delimite
field: one two three field: "moo cow" field: +this
我可以将
字段:
与[a-z]*\:
匹配,但是我似乎无法匹配其余的内容,到目前为止,我的尝试只能捕获我不想做的所有内容。不要忘记,您实际上可以匹配正则表达式中的文字字符串。如果您的模式是这样的:
field\:
您将按字面匹配“field:”,而不匹配任何其他内容。如果您知道它总是按字面匹配
field:
则绝对不需要正则表达式:
var delimiters = new String[] {"field:"};
string[] values = input.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
( # opens a capturing group; the content can later be accessed with Groups[1]
[a-z] # lower-case letter
+ # one or more of them
) # end of capturing group
: # a literal colon
( # opens a capturing group; the content can later be accessed with Groups[2]
(?: # opens a non-capturing group; just a necessary subpattern which we do not
# need later any more
(?! # negative lookahead; this will NOT match if the pattern inside matches
[a-z]+:
# a word followed by a colon; just the same as we used at the beginning of
# the regex
) # end of negative lookahead (not that this does not consume any characters;
# it LOOKS ahead)
. # any character (except for line breaks)
) # end of non-capturing group
* # 0 or more of those
) # end of capturing group
但是,从您的正则表达式中,我假设name
字段
可以变化,只要它位于冒号前面。您可以尝试捕获一个单词,后跟:
,然后捕获下一个单词之前的所有内容(使用前瞻)
正则表达式的解释:
var delimiters = new String[] {"field:"};
string[] values = input.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
( # opens a capturing group; the content can later be accessed with Groups[1]
[a-z] # lower-case letter
+ # one or more of them
) # end of capturing group
: # a literal colon
( # opens a capturing group; the content can later be accessed with Groups[2]
(?: # opens a non-capturing group; just a necessary subpattern which we do not
# need later any more
(?! # negative lookahead; this will NOT match if the pattern inside matches
[a-z]+:
# a word followed by a colon; just the same as we used at the beginning of
# the regex
) # end of negative lookahead (not that this does not consume any characters;
# it LOOKS ahead)
. # any character (except for line breaks)
) # end of non-capturing group
* # 0 or more of those
) # end of capturing group
所以首先我们匹配
anylowercaseword:
。然后我们一次匹配一个以上的字符,每一个都检查这个字符不是另一个小写字母的开头:。通过捕获组,我们可以稍后分别找到字段的名称和字段的值。您需要捕获的确切内容是什么?之后的一切,但不包括字段:
?答案可能对他有用,我认为新加入regex的人很难理解这一点…@MarioDeSchaepmeester你可能是对的,但我通常会因为提供了简单的答案而被更有经验的用户责骂:D.@MarioDeSchaepmeester补充了一些相当广泛的解释;)看起来不错!连我都明白。你用什么工具写出来并格式化了吗?我看你只用了5分钟,还是你只是一个打字速度快的人?@MarioDeSchaepmeester我想我只是一个打字速度快的人;)。我都是手工做的。