开发者

Appending and Prepending '#' in an string using only Regex

I am new to regex and recently faced this problem

Appending and Prepending '#' in an string using only Regex

In this suppose we have string such as Hello my name is #First Name# @EducationDetail@

Now I have to append and prepend # in this using only regular expres开发者_开发问答sion so it will become

#Hello my name is #First Name# @EducationDetail@#

Thanks


var res = Regex.Replace(input, @"^(.*)$", "#$1#");


Same idea as polishchuk, but a bit less verbose

var res = Regex.Replace(input, ".+", "#$0#");

Since regular expressions are greedy by default, .+ will capture the entire string. $0 captures the entire match, so there's no reason to use an explicit capture in this case.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜