[英]Remove characters with regex in c#
I am not a regex specialist, so I need some help with this. 我不是正则表达式专家,所以我需要一些帮助。 I have a text file, and I need to remove some trailing delimiters. 我有一个文本文件,我需要删除一些尾随分隔符。 The text file looks like this: 文本文件如下所示:
MSH|^~\&|OAZIS||||20101029135359||ADT^A31|00000015|P|2.3.1||||||ASCII
EVN|A31|20101029135359^^^^||||19900101
So I think the best way is to do a Regex replace? 所以我认为最好的方法是做一个Regex替换? Can anyone help me with this regex? 任何人都可以帮我这个正则表达式吗?
I want to remove all ^
that come before a |
我想删除在|
之前的所有^
So test^A^^|
所以test^A^^|
has to become test^A|
必须成为test^A|
Thanks 谢谢
resultString = Regex.Replace(subjectString, @"\^+\|", "|");
应该照顾好。
I belive your regular expression would look like this... 我相信你的正则表达式看起来像这样......
\^+\|
That should match one ore more '^' followed by a '|'. 这应该匹配一个或多个'^'后跟一个'|'。
The regex to match will be something like : 匹配的正则表达式将是这样的:
^+\\| ^ + \\ |
But its dangerous to use regexes you don't understand (just like any other code !) 但是使用你不理解的正则表达式是危险的(就像任何其他代码一样!)
read some tutorials or you'll miss a lot of things, for example : 阅读一些教程或者你会错过很多东西,例如:
http://www.codeproject.com/KB/dotnet/regextutorial.aspx http://www.codeproject.com/KB/dotnet/regextutorial.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.