我需要编写一个正则表达式来选择字符串中的所有管道值,无论行中的数字是多少。例如:
Hello || everyone |||| reading |||||||| my ||| post | on || stack-overflow ||||||||||
我的想法是需要用单个值替换所有管道组。除管道外的所有东西都应保持不变
我的正则表达能力有限,但到目前为止我已创建:“(\ |)* $”
然而,这只选择最后的管道组而不是其他管道。提前感谢您的帮助。
编辑:我应该注意我在C#中使用Regex.Replace()答案 0 :(得分:1)
您应该使用Regex.Split
代替Regex.Replace
splitArray = Regex.Split(subjectString, @"\|+");
但如果您更喜欢替换:
resultString = Regex.Replace(subjectString, @"\|+", "");
如果你只想留下一根烟斗(我无法从你的问题中解决这个问题),你可以:
resultString = Regex.Replace(subjectString, @"\|+", "|");
答案 1 :(得分:0)
这应该这样做。
string input = "Hello||everyone||||reading ||||||||my|||post|on||stack-overflow||||||||||";
string yourSingleValue = "someValue";
Regex rePipeStripper = new Regex(@"\|+",RegexOptions.None);
string output = rePipeStripper.Replace(input, yourSingleValue);