将正则表达式模式指定为数组的键

发布于 2024-09-11 18:08:29 字数 1131 浏览 3 评论 0原文

我有一个正则表达式数组,并尝试循环遍历文本文档以查找第一个模式,将其指定为数组的键,然后继续查找第二个模式并将其指定为值。每当我遇到模式 1 时,我希望它始终被分配为键,并且在遇到新键之前所有模式 2 匹配都将被分配给第一个键作为值。

文本文档结构:

Subject: sometext

Email: [email protected]

source: www.google.com www.stackoverflow.com www.reddit.com

所以我有一个表达式数组:

$expressions=array(
                'email'=>'(\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b)',
                'url'=>'([A-Za-z][A-Za-z0-9+.-]{1,120}:[A-Za-z0-9/](([A-Za-z0-9$_.+!*,;/?:@&~=-])|%[A-Fa-f0-9]{2}){1,333}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*,;/?:@&~=%-]{0,1000}))?)'
               );

我想循环遍历我的文本文档并匹配电子邮件地址,然后将其指定为数组的键,然后将后面的所有网址指定为值,将输出发送到上面文本将是:

array(
  '[email protected]' => array (
      0 => 'www.google.com',
      1 => 'www.stackoverflow.com',
      2 => 'www.reddit.com'
    )      

I have an array of regular expressions and am trying to loop through a text document to find the first pattern, assign that as the key to an array then continue through find the second pattern and assign that as the value. Whenever I come across pattern 1 I want that to always be assigned as a key and all pattern 2 matches that follow until I come across a new key will be assigned to that first key as values.

Text document structure:

Subject: sometext

Email: [email protected]

source: www.google.com www.stackoverflow.com www.reddit.com

So I have an array of expressions:

$expressions=array(
                'email'=>'(\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b)',
                'url'=>'([A-Za-z][A-Za-z0-9+.-]{1,120}:[A-Za-z0-9/](([A-Za-z0-9$_.+!*,;/?:@&~=-])|%[A-Fa-f0-9]{2}){1,333}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*,;/?:@&~=%-]{0,1000}))?)'
               );

I want to loop through my text document and match the email address then assign that as the key to an array then assign all urls that follow as the values, s the output to the above text would be:

array(
  '[email protected]' => array (
      0 => 'www.google.com',
      1 => 'www.stackoverflow.com',
      2 => 'www.reddit.com'
    )      

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

送舟行 2024-09-18 18:08:29

执行此操作的一种方法是:

$parts = preg_split("/(emailexpr)/",$txt,-1,PREG_SPLIT_DELIM_CAPTURE);

$res = array();

// note: $parts[0] will be everything preceding the first emailexpr match
for ( $i=1; isset($parts[$i]); $i+=2 )
{
    $email = $parts[$i];
    $chunk = $parts[$i+1];
    if ( preg_match_all("/domainexpr/",$chunk,$match) )
    {
        $res[$email] = $match[0];
    }
}

emailexprdomainexpr 替换为您的正则表达式乱码。

One way to do such a thing:

$parts = preg_split("/(emailexpr)/",$txt,-1,PREG_SPLIT_DELIM_CAPTURE);

$res = array();

// note: $parts[0] will be everything preceding the first emailexpr match
for ( $i=1; isset($parts[$i]); $i+=2 )
{
    $email = $parts[$i];
    $chunk = $parts[$i+1];
    if ( preg_match_all("/domainexpr/",$chunk,$match) )
    {
        $res[$email] = $match[0];
    }
}

replace emailexpr and domainexpr with your regexp gibberish.

迷爱 2024-09-18 18:08:29

我会这样做:

$lines = file('input_file', FILE_SKIP_EMPTY_LINES);
$array = array();
foreach($lines as $line) {
  if(preg_match('/^Subject:/', $line) {
    $email = '';
  } elseif(preg_match('/^Email: (.*)$/', $line, $m)) {
    if(preg_match($expressions['email'], $m[1])) {
      $email = $m[1];
    }
  } elseif(preg_match('/^source: (.*)$/', $line, $m) && $email) {
    foreach(explode(' ', $m[1]) as $url) {
      if(preg_match($expressions['url'], $url)) {
        $array[$email][] = $url;
      }
    }
  }
}

I would do:

$lines = file('input_file', FILE_SKIP_EMPTY_LINES);
$array = array();
foreach($lines as $line) {
  if(preg_match('/^Subject:/', $line) {
    $email = '';
  } elseif(preg_match('/^Email: (.*)$/', $line, $m)) {
    if(preg_match($expressions['email'], $m[1])) {
      $email = $m[1];
    }
  } elseif(preg_match('/^source: (.*)$/', $line, $m) && $email) {
    foreach(explode(' ', $m[1]) as $url) {
      if(preg_match($expressions['url'], $url)) {
        $array[$email][] = $url;
      }
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文