C# 正则表达式删除行
我需要在 C# 中应用正则表达式。 该字符串如下所示:
MSH|^~\&|OAZIS||C2M||20110310222404||ADT^A08|00226682|P|2.3||||||ASCII
EVN|A08
PD1
PV1|1|test
我想要做的是删除所有仅包含 3 个字符的行(没有分隔符“|”)。因此在这种情况下,必须删除“PD1”行(第 3 行)。 这可以通过正则表达式实现吗?
谢谢
I need to apply a regex in C#.
The string looks like the following:
MSH|^~\&|OAZIS||C2M||20110310222404||ADT^A08|00226682|P|2.3||||||ASCII
EVN|A08
PD1
PV1|1|test
And what I want to do is delete all the lines that only contain 3 characters (with no delimiters '|'). So in this case, the 'PD1' line (3rd line) has to be deleted.
Is this possible with a regex?
Thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
下面将在没有正则表达式的情况下执行您想要的操作。
这假设您的文件是一个大字符串。它会为您提供另一个字符串,并删除必要的行。
(或者您可以直接使用文件来完成此操作:
您最终会得到一个包含文件中所有正确行的 String[] 。)
The following will do what you want without regular expressions.
This assumes that you have your file as one large string. And it gives you another string with the necessary lines removed.
(Or you could do it with the file directly:
You end up with a String[] with all of the correct lines in your file.)
这:
(?
正则表达式匹配您在我使用的在线正则表达式测试器中想要的内容,但我相信
{4}
实际上应该是{3}
,所以如果没有,请尝试切换它们不适合你。编辑:
这也有效:
\n[^|\n]{3}\n
并且可能更接近您正在寻找的内容。编辑2:
括号中的数字肯定是
{3}
,在家测试过。This:
(?<![|])[^\n]{4}\n
Regex matched what you wanted in the online regex tester I used, however I believe that the
{4}
should actually be a{3}
, so try switching them if it doesn't work for you.EDIT:
This also works:
\n[^|\n]{3}\n
and is probably closer to what you are looking for.EDIT 2:
The number is brackets is definitely
{3}
, tested it at home.为什么不直接获取文件的句柄,创建一个临时输出文件,然后逐行运行呢?如果有一行有3个字符,则跳过它。如果文件可以完全保存在内存中,那么也许可以使用 GetLines() (我认为这就是该方法的名称)来获取逐行表示文件的字符串数组。
why not just get a handle to the file, make a temporary output file, and run through the lines one by one. If there is a line with 3 characters, just skip it. If the file can be held in memory entirely, then maybe use the GetLines() (i think that's what the method is called) to get an array of strings that represents the file line by line.
这三个角色总是独自一人在一条线上吗?如果是这样,您可以使用字符串开头/字符串结尾标记。
下面是一个正则表达式,它匹配字符串中单独存在的三个字符:
\A 是字符串的开头。
\z 是字符串的结尾。
。是任意字符,{3} 出现 3 次
Are the three characters always going to be by themselves on a line? If so, you can use beginning of string/end of string markers.
Here's a Regex that matches three characters that are by themselves on a string:
\A is the start of the string.
\z is the end of the string.
. is any character, {3} with 3 occurrences
^ - 行的开头。
\w - 单词字符
{3} - 恰好重复 3 次
$ - 行尾
^ - start of line.
\w - word character
{3} - repreated exactly 3 times
$ - end of line
这只是我迄今为止看到的解决方案的一般观察。最初的问题包括评论“删除所有仅包含3个字符的行”[我的重点]。我不确定你的意思是否是字面意义上的“只有 3 个字符”,但如果你这样做了,你可能想要将提议的解决方案的逻辑更改为
……
以防万一包含 2 个字符的行确实有效,例如。 (与提议的正则表达式解决方案的想法相同。)
Just a general observation from the solutions I've seen posted so far. The original question included the comment "delete all the lines that only contain 3 characters" [my emphasis]. I'm not sure if you meant literally "only 3 characters", but in case you did, you may want to change the logic of the proposed solutions from things like
to
...just in case lines with 2 characters are indeed valid, for example. (Same idea for the proposed regex solutions.)
此正则表达式将识别满足排除条件
^[^|]{3}$
的行,然后只需迭代所有行(带数据)并检查哪些行满足排除条件即可。比如像这样。This regex will identify the lines that meet your exclusion criteria
^[^|]{3}$
then it's just a matter of iterating over all lines (with data) and checking which ones meet exclusion criteria. Like this for instance.问题有点模糊。
如前所述,答案类似于这样的
(?:^|(?<=\n))[^\n|]{3}(?:\n|$)
,它允许空格在比赛中。因此
"#\t)"
也将被删除。要将字符限制为视觉字符(非空白),您可以使用
(?:^|(?<=\n))[^\s|]{3}(?:\n|$)
其中不允许空格。
对于这两种情况,上下文都是单个字符串,替换为 '' 和全局。
Perl 中的示例上下文:
s/(?:^|(?<=\n))[^\n|]{3}(?:\n|$)//g
The question is a little vague.
As stated, the answer is something like this
(?:^|(?<=\n))[^\n|]{3}(?:\n|$)
which allows whitespace in the match.So
"#\t)"
will also be deleted.To limit the characters to visual (non-whitespace), you could use
(?:^|(?<=\n))[^\s|]{3}(?:\n|$)
which doesent allow whitespace.
For both the context is a single string, replacement is '' and global.
Example context in perl:
s/(?:^|(?<=\n))[^\n|]{3}(?:\n|$)//g
试试这个:
try this:
您可以使用正则表达式
[a-zA-Z0-9] 来匹配任何字符或数字
{3} 将匹配 3 的确切数字
You can do it Using Regex
[a-zA-Z0-9] will match any character or number
{3} will match exact number of 3