简体   繁体   English

从数组解析正则表达式

[英]parse regex from array

I am trying to parse out IP addresses from Outlook email headers. 我正在尝试从Outlook电子邮件标头解析IP地址。 I've started writing some stuff in C# (because that is the example I was leveraging) and have come up with something close. 我已经开始用C#编写一些东西(因为这就是我所利用的示例),并且已经提出了一些建议。

I can split the headers with the string lines[] = Regex.Split(headers, @"\\r\\n"); 我可以用字符串lines [] = Regex.Split(headers,@“ \\ r \\ n”);分割标题。 command okay, but when I try to iterate through the lines[] array, my regex for IP address fails and does not store the value in a second array: 命令可以,但是当我尝试遍历lines []数组时,我的IP地址正则表达式失败,并且不将值存储在第二个数组中:

Code: 码:

private void button1_Click(object sender, EventArgs e)
    {
        // use a string constant to define the mapi property
        string PidTagTransportMessageHeaders = @"http://schemas.microsoft.com/mapi/proptag/0x007D001E";
        string mypattern = @"(#{1,3}\.)(#{1,3}\.)([0-9]{1,3}\.)([0-9]{1,3})";
        // string[] ip = Regex.Split(lines[i], (@"(\(|\[)(#{1,3}\.)(#{1,3}\.)([0-9]{1,3}\.)([0-9]{1,3})(\)|\])"));

        // get a handle on the current message
        Outlook.MailItem message = (Outlook.MailItem)this.OutlookItem;

        // use the property accessor to retreive the header
        string headers = string.Empty;

        try
        {
            headers = (string)message.PropertyAccessor.GetProperty(PidTagTransportMessageHeaders);
        }
        catch { 
        }

        //  if getting the internet headers is successful, put into textbox
        string[] lines = Regex.Split(headers, "\r\n");

        Regex regexObj = new Regex(mypattern);

        for (int i = 0; i < lines.Length; i++)
        {
            MatchCollection matches = regexObj.Matches(lines[i]);                       

        }            
        //eventually write the found IP array into textBox1.Text
       textBox1.Text = headers;
        }
    }
}

Any help or suggestions? 有什么帮助或建议吗?

Change your # 's to \\d 's: 将您的#更改为\\d

string mypattern = @"(\d{1,3}\.)(\d{1,3}\.)(\d{1,3}\.)(\d{1,3})";

Note that a more accurate IPv4 address capture regular expression would be something like: 请注意,更准确的IPv4地址捕获正则表达式将类似于:

\b(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b

...or at least add word boundaries... ...或至少添加单词边界...

\\b(\\d{1,3}\\.)(\\d{1,3}\\.)(\\d{1,3}\\.)(\\d{1,3})\\b

For a simple IPv6 (standard) I like: 对于简单的IPv6(标准),我喜欢:

(?<![:.\w])(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}(?![:.\w])

IPAddress.Parse方法不会重新发明轮子。

如果您要匹配IPv4,请尝试使用此野兽,它应与实际的IPv4相当接近, \\ b表示单词的开头和结尾,因此您应该能够删除它们并进行调整根据您的标题格式获取您的心脏内容

\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b

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

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