你可以试试
向前看
向后看
(?<=[A-Za-z][A-Za-z0-9]*\s*\(\s*)[A-Z0-9]{2,3}(?=\s*\))
我们有简单的
比赛
感兴趣的
[A-Z0-9]{2,3}
2
到
3
大写字母
数字
. 但是这个
比赛
应该是
之后
Swipe(
或
Formula1(
)以及
之前
)
. 假设
成为一个
标识符
(?<= ) - group, behind: should appear before the match; will not be included into it
[A-Za-z] - one letter (Formula1)
[A-Za-z0-9]* - letters or digits, zero or more
\s* - whitespaces (spaces, tabultalions) - zero or more
匹配
[A-Z0-9]{2,3} - Capital letters or digits from 2 to 3 characters
最后我们应该看看
向前地
为了找出右括号:
(?= ) - group, ahead: should appear before the match; will not be included into it
\s* - zero or more whitespaces (spaces, tabulations etc)
\) - closing parenthesis (escaped)
合并,我们有
(?<=[A-Za-z][A-Za-z0-9]*\s*\(\s*) -- Behind:
-- Letter, zero or more letters or digits, parenthesis
[A-Z0-9]{2,3} -- Value to match (2..3 capital letters or digits)
(?=\s*\) -- Ahead:
-- Closing parenthesis
(?<=[A-Za-z][A-Za-z0-9]*\s*\(\s*)[A-z0-9]{2,3}(?=\s*\)
看到了吗
https://www.regular-expressions.info/lookaround.html
有关详细信息
C级#
代码:
string source = @"Swipe(YN1) + Avg(DNA) * 0.5";
string argument = "calculate";
string result = Regex.Replace(
source,
@"(?<=[A-Za-z][A-Za-z0-9]*\s*\(\s*)[A-Z0-9]{2,3}(?=\s*\))",
match => match.Value + $", {argument}");
Console.Write(result);
Swipe(YN1, calculate) + Avg(DNA, calculate) * 0.5