0\" where I want to replace _2 with string.empty using Regex. My expression is ((_)[^_]*)\\w(?=[\\s=!><]) which in both Regulator and Expresso gives me _2. However," />
开发者

Replacing _x with string.empty using Regex.Replace

I have a string "region_2>0" where I want to replace _2 with string.empty using Regex.

My expression is ((_)[^_]*)\w(?=[\s=!><]) which in both Regulator and Expresso gives me _2. However, the code(c#):

Regex.Match(legacyExpression, "((_)[^_]*)\\w(?=[\\s=!><])").Value

gives me "_2>0", which also causes the replace to be wrong (It returns "region" since removing the whole "_2>0" instead of "_2". The result I want is "region>0". Shouldn't the code and the regex programs give the same results? And how can I get it to work?

(Note the string is not static, it could be in many different forms, but the rule is I want to replace the last _X in the string with string.empty.

Th开发者_StackOverflow中文版anks!


I copied your code as is into the new project:

static void Main(string[] args)
{
    var legacyExpression = "region_2>0";
    var rex = Regex.Match(legacyExpression, "((_)[^_]*)\\w(?=[\\s=!><])").Value;
    Console.WriteLine(rex);
    Console.ReadKey();
}

The output is _2.


I think this could work

(_\d+)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜