开发者

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

开发者 https://www.devze.com 2023-03-18 11:40 出处:网络
I am new to regex and recently faced this problem 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

精彩评论

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

关注公众号