简体   繁体   English

使用正则表达式检查给定字符串中是否存在特定段

[英]Using regular expressions check if a particular segment exist in a given string

There are segments in the below-mentioned string.下面提到的字符串中有段。 Each segment is started with a tilt(~) sign and I want to check if there exists a segment in which the AAA segment appears and on its 3rd index a number 63 is present.每个段都以倾斜(〜)符号开始,我想检查是否存在出现 AAA 段的段,并且在其第三个索引上是否存在数字 63。

ISA*ABC**TODAY*ALEXANDER GONZALEZ~HL*CDD*DKKD*S~EB*1*AKDK**DDJKJ~AAA*Y**50*P~AAA*N**50*P~AAA*N**63*C~AAA*N**50*D~AAA*N**45*D

I want to do it with a regular expression to avoid lengthy coding.我想用正则表达式来避免冗长的编码。 I have tried and come up with this (~AAA) to check if this segment exists or not but because I am new to regular expressions I don't know how to check if 63 appears on the 3rd index or not?我已经尝试提出这个(~AAA)来检查这个段是否存在,但是因为我是正则表达式的新手,我不知道如何检查 63 是否出现在第三个索引上? If anyone can help I will be very thankful.如果有人可以提供帮助,我将非常感激。

I have to agree with Sebastian's comment.我必须同意塞巴斯蒂安的评论。 This can be accomplished using simple Split operations.这可以使用简单的Split操作来完成。

    private static bool Check(string input)
    {
        int count = 0;
        foreach (string segment in input.Split('~'))
        {
            string[] tokens = segment.Split('*');
            if (tokens[0] == "AAA")
            {
                count++;
                if (count == 3)
                {
                    if (tokens[3] == "63") return true;
                    else return false;
                }
            }
        }
        return false;
    }

EDIT: Since you want fewer lines of codes, how about LINQ?编辑:既然你想要更少的代码行,那么 LINQ 怎么样?

    private bool Check(string input)
    {
        return input.Split('~').Select(x => x.Split('*')).Any(x => x.Length >= 4 && x[0].Equals("AAA") && x[3].Equals("63"));
    }

EDIT2: For completeness, here's a Regex solution as well: EDIT2:为了完整起见,这里还有一个正则表达式解决方案:

    private bool Check(string input)
    {
        return Regex.IsMatch(input, @".*~AAA\*([^\*~]*\*){2}63.*");
    }

There are many , many , sites and tools that can help you with regex.很多很多网站和工具可以帮助您使用正则表达式。 Google can help you further.谷歌可以进一步帮助你。

   string s = "ISA*ABC**TODAY*ALEXANDER GONZALEZ~HL*CDD*DKKD*S~EB*1*AKDK**DDJKJ~AAA*Y**50*P~AAA*N**50*P~AAA*N**63*C~AAA*N**50*D~AAA*N*   *45*D";
   string[] parts = s.Split('~');
   for (int i = 0; i < parts.Count(); i++)
   {
      MessageBox.Show("part "+i.ToString()+" contains 63: "+parts[i].Contains("63").ToString());
   }

this is just an example.这只是一个例子。 you can do a lot more, I checked existance of 63 in any segment.你可以做更多,我检查了任何部分中是否存在 63。
so if you exactly want to check if segment 3 the code would be:因此,如果您确实想检查第 3 段代码是否为:

bool segment3Contains63 = s.Slplit('~')[3].Contains("63");

You could use this:你可以使用这个:

~AAA\*[A-Z]\*\*63

Note the \ is used to escape the * and [AZ] matches any uppercase alphabetical character.注意 \ 用于转义 * 和 [AZ] 匹配任何大写字母字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM