正则表达式:匹配IP地址,除非前面有某些字符?

时间:2015-06-12 04:58:32

标签: python regex

这个正则表达式([a-z0-9]{1,4}:+){3,5}[a-z0-9]{1,4}|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}几乎完成了仅匹配IP地址(v4和v6)的工作,但不幸的是,对于下面的文本和类似文本,它还会选择中的字段粗体

  

来自mail.example.com(example.com。[213.239.250.131])by   带有ESMTPS ID的mx.google.com   xc4si15480310lbb.82.2 014.10.26.06 .16.58 for   (version = TLSv1.2 cipher = ECDHE-RSA-AES128-GCM-SHA256 bits = 128/128);   太阳,2014年10月26日06:16:58 -0700(太平洋时间)

     

收到:来自ssservices1-1(192.168.20.142)mail.supershuttle.com   (192.168.20.110)Microsoft SMTP服务器ID 14.2.347.0 ;星期二,21岁   2015年4月

     

收到:来自ssservices1-1(192.168.20.142)mail.supershuttle.com   (192.168.20.110)与Microsoft SMTP(TLS)服务器ID 14.2.347.0 ;   2015年4月21日星期二

     

收到:来自plug.mysitehosted.com(plug.mysitehosted.com   [10.248.1.153])       (使用带密码的TLSv1 DHE-RSA-AES256-SHA)       通过 0.0.0.0 :2500(trex / 5.0.19);       星期二,2014年3月11日06:14:03 GMT

什么是最好的接近(我将使用Python)省略这些匹配?两个前面是文本'id',但在第一种情况下,不是直接在它之前。

2 个答案:

答案 0 :(得分:4)

([a-z0-9]{1,4}:+){3,5}[a-z0-9]{1,4}|(?<!id )(?<!\.)\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b(?!\.)

您可以尝试此操作。通过lookaheads我们确保IP地址不在.之前或之后。请参阅演示。

https://regex101.com/r/hI0qP0/3

答案 1 :(得分:2)

作为附加提示,您可以添加正向前向和向后查找。它们可以让你在不消耗任何东西的情况下查看比赛前后的内容。您的IP始终被()[]包围,因此您可以尝试:

(?<=\[|\()your regex(?=\]|\))

这将匹配your regex中的(your regex) [your regex]

<?php
 
if(isset($_POST['email'])) {
    $email_to = "cdhulf@bellsouth.net";
    $email_subject = "Portfolio site email";
 
    function died($error) {
        echo "We are very sorry, but there were error(s) found with the information you provided. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }
 
// validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the information you provided.');       
    }
 
    $first_name = $_POST['first_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required
 
    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
 
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The email address you entered does not appear to be valid.<br />';
  }
 
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The name you entered does not appear to be valid.<br />';
  }
 
  if(strlen($comments) < 2) {
    $error_message .= 'The information that you entered do not appear to be valid.<br />';
  }
 
  if(strlen($error_message) > 0) {
    died($error_message);
  }
 
    $email_message = "Form details below.\n\n";
 
function clean_string($string) {
  $bad = array("\\","content-type","href");
  return str_replace($bad,"",$string);
  }
 
    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";
 
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
 
@mail($email_to, $email_subject, $email_message);  

header("location:javascript://history.go(-1)");
}
?>
<?php