Need C# Regex for replacing spaces inside of strings
Using C#, I need a some code to use regular expressions to replace spaces inside of quotes with a pipe character (|). problem is that the string could contain multiple quoted expressions and I only want the spaces inside of quotes .
I tried a few things but I am struggling with how to handle the variable number of words that could be inside of quotes, amongst other things.
Here is some examples of what may be input, and the required output:
"word1 word2"
-> "word1|word2"
"word1 word2" word3 "word4 word5"
-> "word1|word2" word3 "word4|word5"
word1 "word2 word3"
-> word1 "word2|word3"
Any help greatly appreciated, and hopefully I will learn about regular expressions.
使用正则表达式查找引号,并使用普通的替换来替换空格:
str = Regex.Replace(str, @"""[^""]+""", m => m.Value.Replace(' ', '|'));
There is a helpful webiste to test stuff like this, it's called reFiddle
http://refiddle.com
What i would do is to use this
http://refiddle.com/288
/["][^"]+["]/g
To get the strings that are inside of quotes, then just do a replace on those returned strings, and you should be golden.
链接地址: http://www.djcxy.com/p/51386.html上一篇: DistinctBy当distinct属性是一个列表
下一篇: 需要C#Regex来替换字符串中的空格