开发者

C# Regular Expression - Adding Special Characters

开发者 https://www.devze.com 2023-03-02 19:44 出处:网络
I was wonderi开发者_StackOverflow中文版ng what is the best way to include backslash and other special characters in a group?

I was wonderi开发者_StackOverflow中文版ng what is the best way to include backslash and other special characters in a group?

example:

"message":"\"rock on\" \\,,/,[-_-]";

help me on my regex

[a-zA-Z0-9 \\-~!@#$%^*()_+{}:|?`;',\\./\\[\\]]+


Just escape those that need to be escaped and add those, that don't need to:

[a-zA-Z0-9 \\\-~!@#$%^*()_+{}:|"?`;',./[\]]+

To elaborate a bit:
You only need to escape \, ] and - inside a character group.

Using C#, it would look like this:

Regex rx = new Regex(@"[a-zA-Z0-9 \\\-~!@#$%^*()_+{}:|""?`;',./[\]]+");
0

精彩评论

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

关注公众号