我使用"[a-z][a-z0-9]*"查找子字符串:
" as4s“-找到as4s "s+sd4“- found s,sd4 “(4 asd悲伤)”-找到了asd,悲伤 "10asd“-发现asd
我需要改变这种体验,这样结果就会是:
" as4s“-找到as4s "s+sd4“- found s,sd4 “(4 4asd)”-发现悲伤 "10asd“-一无所获
可以使用此代码测试表达式:
using System.Text.RegularExpressions;
string input = "A*10+5.01E+10";
Regex r = new Regex("[a-zA-Z][a-zA-Z\d]*");
var identifiers = new Dictionary<string, string>();
MatchEvaluator me = delegate(Match m)
{
Console.WriteLine(m);
var variableName = m.ToString();
if (identifiers.ContainsKey(variableName))
{
return identifiers[variableName];
}
else
{
i++;
var newVariableName = "i" + i.ToString();
identifiers[variableName] = newVariableName;
return newVariableName;
}
};
input = r.Replace(input, me);发布于 2014-12-29 07:05:44
发布于 2014-12-29 07:10:48
(?<!\d)(\b[a-z][a-z0-9]*)尝试this.Grab capture.See演示。
https://regex101.com/r/gX5qF3/7
NODE EXPLANATION
--------------------------------------------------------------------------------
(?<! look behind to see if there is not:
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
[a-z] any character of: 'a' to 'z'
--------------------------------------------------------------------------------
[a-z0-9]* any character of: 'a' to 'z', '0' to '9'
(0 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
) end of \1https://stackoverflow.com/questions/27684742
复制相似问题