var str = "=IIF(IsNothing(Fields!Metadata1.Value),"N/A",Fields!Metadata1.Value)";
var regexp = new Regex(@"Fields!(\w+)");
var matches = regexp.Matches(str);
The matches will have "Fields!Metadata1" while I need to get "Metadata1"
匹配将有“Fields!Metadata1”,而我需要获得“Metadata1”
What shall I change?
我该怎么改变?
3 个解决方案
#1
7
don't know c# syntax, for regex, try:
不知道c#语法,对于正则表达式,请尝试:
(?<=Fields!)\w*
EDIT add short explanation:
编辑补充简短说明:
(?<=foo)bar
matches bar
only if bar
is following foo
(foo is not gonna in match result) (?<=..)
is positive look behind, zero-width assertion. google it for details.
(?<= foo)bar只有当bar跟随foo时才匹配bar(foo不会出现在匹配结果中)(?<= ..)是正面看后面,零宽度断言。 google了解详情。
#2
9
What you need is called lookbehind/lookahead. Regex has this feature, you can specify what follows (or in this case preceeds) the currently matched string.
你需要什么叫做lookbehind / lookahead。正则表达式具有此功能,您可以指定当前匹配的字符串后面(或在此情况下)。
[0-9](?=[a-z])
will match any one digit followed by a lowercase letter.[0-9](?![a-z])
will match any one digit NOT followed by a lowercase letter. (this is called a negative lookahead)(?<=[a-z])[0-9]
will match any one digit preceeded by a lowercase letter.(?<![a-z])[0-9]
will match any one digit NOT preceeded by a lowercase letter.(this is called a negative lookbehind)
[0-9](?= [a-z])将匹配任何一个数字后跟一个小写字母。 [0-9](?![a-z])将匹配任何一个数字NOT后跟一个小写字母。 (这称为负向前瞻)(?<= [a-z])[0-9]将匹配以小写字母开头的任何一个数字。 (?
With that in mind, the c# expression you want is:
考虑到这一点,你想要的c#表达式是:
var str = "=IIF(IsNothing(Fields!Metadata1.Value),"N/A",Fields!Metadata1.Value)";
var regexp = new Regex(@"(?<=Fields!)(\w+)");
var matches = regexp.Matches(str);
EDIT: this is good for you if you DON'T want to match the "Fields!" part. A slightly different task is if you want to match it, but you also need the second part's value. In which case, I recommend using named groups, which will capture the entire thing, but you can get the part from the Groups collection. The regex in that case will be: Fields!(?'fieldName'\w+)
, and the c# usage is
编辑:如果你不想匹配“菲尔兹”,这对你有好处!部分。稍微不同的任务是如果你想匹配它,但你还需要第二部分的值。在这种情况下,我建议使用命名组,它将捕获整个事物,但您可以从Groups集合中获取该部分。在这种情况下的正则表达式将是:Fields!(?'fieldName'\ w +),c#用法是
var str = "=IIF(IsNothing(Fields!Metadata1.Value),"N/A",Fields!Metadata1.Value)";
var regexp = new Regex(@"Fields!(?'fieldName'\w+)");
var matches = regexp.Matches(str);
foreach (Match m in matches)
{
var fieldName = m.Groups["fieldName"].Value;
//...
}
#3
2
maybe you can try this...
也许你可以尝试这个......
var match = Regex.Match(item, @"!(\w+)");
var match = Regex.Match(item,@“!(\ w +)”);
then get the match.Groups[1].Value
然后得到match.Groups [1] .Value
#1
7
don't know c# syntax, for regex, try:
不知道c#语法,对于正则表达式,请尝试:
(?<=Fields!)\w*
EDIT add short explanation:
编辑补充简短说明:
(?<=foo)bar
matches bar
only if bar
is following foo
(foo is not gonna in match result) (?<=..)
is positive look behind, zero-width assertion. google it for details.
(?<= foo)bar只有当bar跟随foo时才匹配bar(foo不会出现在匹配结果中)(?<= ..)是正面看后面,零宽度断言。 google了解详情。
#2
9
What you need is called lookbehind/lookahead. Regex has this feature, you can specify what follows (or in this case preceeds) the currently matched string.
你需要什么叫做lookbehind / lookahead。正则表达式具有此功能,您可以指定当前匹配的字符串后面(或在此情况下)。
[0-9](?=[a-z])
will match any one digit followed by a lowercase letter.[0-9](?![a-z])
will match any one digit NOT followed by a lowercase letter. (this is called a negative lookahead)(?<=[a-z])[0-9]
will match any one digit preceeded by a lowercase letter.(?<![a-z])[0-9]
will match any one digit NOT preceeded by a lowercase letter.(this is called a negative lookbehind)
[0-9](?= [a-z])将匹配任何一个数字后跟一个小写字母。 [0-9](?![a-z])将匹配任何一个数字NOT后跟一个小写字母。 (这称为负向前瞻)(?<= [a-z])[0-9]将匹配以小写字母开头的任何一个数字。 (?
With that in mind, the c# expression you want is:
考虑到这一点,你想要的c#表达式是:
var str = "=IIF(IsNothing(Fields!Metadata1.Value),"N/A",Fields!Metadata1.Value)";
var regexp = new Regex(@"(?<=Fields!)(\w+)");
var matches = regexp.Matches(str);
EDIT: this is good for you if you DON'T want to match the "Fields!" part. A slightly different task is if you want to match it, but you also need the second part's value. In which case, I recommend using named groups, which will capture the entire thing, but you can get the part from the Groups collection. The regex in that case will be: Fields!(?'fieldName'\w+)
, and the c# usage is
编辑:如果你不想匹配“菲尔兹”,这对你有好处!部分。稍微不同的任务是如果你想匹配它,但你还需要第二部分的值。在这种情况下,我建议使用命名组,它将捕获整个事物,但您可以从Groups集合中获取该部分。在这种情况下的正则表达式将是:Fields!(?'fieldName'\ w +),c#用法是
var str = "=IIF(IsNothing(Fields!Metadata1.Value),"N/A",Fields!Metadata1.Value)";
var regexp = new Regex(@"Fields!(?'fieldName'\w+)");
var matches = regexp.Matches(str);
foreach (Match m in matches)
{
var fieldName = m.Groups["fieldName"].Value;
//...
}
#3
2
maybe you can try this...
也许你可以尝试这个......
var match = Regex.Match(item, @"!(\w+)");
var match = Regex.Match(item,@“!(\ w +)”);
then get the match.Groups[1].Value
然后得到match.Groups [1] .Value