C# 正则表达式删除行

发布于 2024-10-21 12:08:14 字数 252 浏览 2 评论 0原文

我需要在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

天荒地未老 2024-10-28 12:08:14

下面将在没有正则表达式的情况下执行您想要的操作。

String inputString;
String resultingString = "";
for(var line in inputString.Split(new String[]{"\n"})) {
    if (line.Trim().Length > 3 || line.Contains("|"))
        resultingString += line + "\n";
}

这假设您的文件是一个大字符串。它会为您提供另一个字符串,并删除必要的行。

(或者您可以直接使用文件来完成此操作:

string[] goodLines = 
    // read all of the lines of the file
    File.ReadLines("fileLocation").
        // filter out the ones you want
        Where(line => line.Trim().Length > 3 || line.Contains("|")).ToArray();

您最终会得到一个包含文件中所有正确行的 String[] 。)

The following will do what you want without regular expressions.

String inputString;
String resultingString = "";
for(var line in inputString.Split(new String[]{"\n"})) {
    if (line.Trim().Length > 3 || line.Contains("|"))
        resultingString += line + "\n";
}

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:

string[] goodLines = 
    // read all of the lines of the file
    File.ReadLines("fileLocation").
        // filter out the ones you want
        Where(line => line.Trim().Length > 3 || line.Contains("|")).ToArray();

You end up with a String[] with all of the correct lines in your file.)

掀纱窥君容 2024-10-28 12:08:14

这:
(?
正则表达式匹配您在我使用的在线正则表达式测试器中想要的内容,但我相信 {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.

痕至 2024-10-28 12:08:14

为什么不直接获取文件的句柄,创建一个临时输出文件,然后逐行运行呢?如果有一行有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.

2024-10-28 12:08:14

这三个角色总是独自一人在一条线上吗?如果是这样,您可以使用字符串开头/字符串结尾标记。

下面是一个正则表达式,它匹配字符串中单独存在的三个字符:

\A.{3}\z

\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.{3}\z

\A is the start of the string.
\z is the end of the string.
. is any character, {3} with 3 occurrences

小帐篷 2024-10-28 12:08:14

^ - 行的开头。
\w - 单词字符
{3} - 恰好重复 3 次
$ - 行尾

^\w{3}$

^ - start of line.
\w - word character
{3} - repreated exactly 3 times
$ - end of line

^\w{3}$
殤城〤 2024-10-28 12:08:14

这只是我迄今为止看到的解决方案的一般观察。最初的问题包括评论“删除所有包含3个字符的行”[我的重点]。我不确定你的意思是否是字面意义上的“只有 3 个字符”,但如果你这样做了,你可能想要将提议的解决方案的逻辑更改为

   if (line.Trim().Length > 3 ...)

……

   if (line.Trim().Length != 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

   if (line.Trim().Length > 3 ...)

to

   if (line.Trim().Length != 3 ...)

...just in case lines with 2 characters are indeed valid, for example. (Same idea for the proposed regex solutions.)

娇妻 2024-10-28 12:08:14

此正则表达式将识别满足排除条件 ^[^|]{3}$ 的行,然后只需迭代所有行(带数据)并检查哪些行满足排除条件即可。比如像这样。

foreach(Match match in Regex.Matches(data, @"^.+$")
{
  if (!Regex.IsMatch(match.Value, @"^[^|]{3}$"))
  {
     // Do Something with legitamate match.value like write line to target file.
  }
}

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.

foreach(Match match in Regex.Matches(data, @"^.+$")
{
  if (!Regex.IsMatch(match.Value, @"^[^|]{3}$"))
  {
     // Do Something with legitamate match.value like write line to target file.
  }
}
贱贱哒 2024-10-28 12:08:14

问题有点模糊。

如前所述,答案类似于这样的

(?:^|(?<=\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

二智少女猫性小仙女 2024-10-28 12:08:14

试试这个:

text = System.Text.RegularExpressions.Regex.Replace(
        text, 
        @"^[^|]{3}(?:\r\n|[\r\n]|$)", 
        "", 
        System.Text.RegularExpressions.RegexOptions.Multiline);

try this:

text = System.Text.RegularExpressions.Regex.Replace(
        text, 
        @"^[^|]{3}(?:\r\n|[\r\n]|$)", 
        "", 
        System.Text.RegularExpressions.RegexOptions.Multiline);
呆萌少年 2024-10-28 12:08:14

您可以使用正则表达式

string output = Regex.Replace(input, "^[a-zA-Z0-9]{3}$", "");

[a-zA-Z0-9] 来匹配任何字符或数字
{3} 将匹配 3 的确切数字

You can do it Using Regex

string output = Regex.Replace(input, "^[a-zA-Z0-9]{3}$", "");

[a-zA-Z0-9] will match any character or number
{3} will match exact number of 3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文