开发者

regex for formatting words

开发者 https://www.devze.com 2022-12-19 06:41 出处:网络
how can I format below string Address1=+1234+block+of+XYZ+Street+Address2=+Santa+Fe+Springs+State=+California

how can I format below string

Address1=+1234+block+of+XYZ+Street+Address2=+Santa+Fe+Springs+State=+California

to string

Address1=+1234+block+of+XYZ+Street+&Address2=+Santa+Fe+Springs+&State=+California

The below regex doesnt work properly.could someone f开发者_如何转开发ix this?

string inputString = "Address1=+1234+block+of+XYZ+Street+Address2=+Santa+Fe+Springs+State=+California";
string outString = Regex.Replace(inputString,@"([\s])([a-zA-Z0-9]*)(=)","&$1");


I think you want this

Regex.Replace(inputString,@"\+([a-zA-Z0-9]+)=","+&$1=")

Or this if you want to allow any character other than + & = in keywords.

Regex.Replace(inputString,@"\+([^+&=]+)=","+&$1=")


If all you want to do is prefix "Address2" and "State" by an ampersand:

Regex.Replace(inputString, "(?=Address2=|State=)", "&");
0

精彩评论

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

关注公众号