简体   繁体   English

preg_match-创建数组的正则表达式

[英]preg_match - Regular Expression To Create Array

My data - 我的资料-

{'/Users/aaron/Applications/developer-vagrant/web/g.php': {'total': 22}}
{'/Users/aaron/.vim/autoload/timetap.vim': {'total': 0}}
{'/Users/aaron/.vimrc': {'total': 5}}
{'/Users/aaron/Documents/Programming/PHP/TimeTapCLI/composer.json': {'total': 144}}
{'/Users/aaron/Documents/Programming/PHP/TimeTapCLI/timetap.php': {'total': 351}}
{'/Users/aaron/Box/linux/.vim/autoload/timetap.vim': {'total': 37}}
{'/Users/aaron/Box/cats.tex': {'total': 184}}

I am attempting to create a regular expression so I can convert the above into an array using preg_match. 我试图创建一个正则表达式,以便可以使用preg_match将以上内容转换为数组。 I want the data to look like - 我希望数据看起来像-

I want an array of all of the data So I believe it should look like below- 我想要所有数据的数组,所以我认为它应该如下图所示-

 array (
   [0] => array (
      [0] => '/Users/aaron/Box/cats.tex'
      [1] => array (
                  [total] =>'184'
             )
   }
 }

My attempt at preg_match - 我尝试preg_match-

$subject = file_get_contents('/Users/aaron/.timetap/full.db');
$pattern = '{...}';
preg_match($pattern, substr($subject,3), $matches, PREG_OFFSET_CAPTURE);

What would the pattern be in order to take the above data and turn it into an array in PHP? 为了获取上述数据并将其转换为PHP数组,模式是什么? Is there a PHP function that could convert this into an Array without using preg_match? 是否有PHP函数可以在不使用preg_match的情况下将其转换为数组?

You're regex doesn't make sense as you have it. 正则表达式没有意义。 For one thing you are missing delimiters. 一方面,您缺少分隔符。 The { , } , and . {}. are all special regex characters so they should be escaped. 都是特殊的正则表达式字符,因此应将其转义。 This also looks like a JSON data structure so the JSON functions might be of use to you. 这也看起来像JSON数据结构,因此JSON函数可能对您有用。 If you still want to go REGEX here's how I'd do it presuming your data structure is consistent. 如果您仍然想使用REGEX,请按照以下假定您的数据结构一致的方式进行操作。

<?php
$string = "{'/Users/aaron/Applications/developer-vagrant/web/g.php': {'total': 22}}
{'/Users/aaron/.vim/autoload/timetap.vim': {'total': 0}}
{'/Users/aaron/.vimrc': {'total': 5}}
{'/Users/aaron/Documents/Programming/PHP/TimeTapCLI/composer.json': {'total': 144}}
{'/Users/aaron/Documents/Programming/PHP/TimeTapCLI/timetap.php': {'total': 351}}
{'/Users/aaron/Box/linux/.vim/autoload/timetap.vim': {'total': 37}}
{'/Users/aaron/Box/cats.tex': {'total': 184}}";
$pattern = '~^\{(.*)\}$~m';
$data[] = preg_replace_callback($pattern, function($matches) {
    global $output_data;
    preg_match("~'(.*?)'\s*:\s*\{'(.*?)'\s*:\s*(\d+)\}~", $matches[1], $output);
    $output_data[$output[1]] = array($output[2] => $output[3]);
}, $string);
print_r($output_data);

Output: 输出:

Array
(
    [/Users/aaron/Applications/developer-vagrant/web/g.php] => Array
        (
            [total] => 22
        )

    [/Users/aaron/.vim/autoload/timetap.vim] => Array
        (
            [total] => 0
        )

    [/Users/aaron/.vimrc] => Array
        (
            [total] => 5
        )

    [/Users/aaron/Documents/Programming/PHP/TimeTapCLI/composer.json] => Array
        (
            [total] => 144
        )

    [/Users/aaron/Documents/Programming/PHP/TimeTapCLI/timetap.php] => Array
        (
            [total] => 351
        )

    [/Users/aaron/Box/linux/.vim/autoload/timetap.vim] => Array
        (
            [total] => 37
        )

    [/Users/aaron/Box/cats.tex] => Array
        (
            [total] => 184
        )

)

Here are links to the information on the functions/modifiers I've used. 这是我使用的功能/修饰符信息的链接。

  1. http://php.net/manual/en/reference.pcre.pattern.modifiers.php http://php.net/manual/en/reference.pcre.pattern.modifiers.php
  2. http://php.net/manual/en/function.preg-replace-callback.php http://php.net/manual/en/function.preg-replace-callback.php
  3. http://php.net/manual/en/function.preg-match.php http://php.net/manual/en/function.preg-match.php

I'll do a write up of the parts used here in a bit. 我将对这里使用的部分进行一些记录。 If you have particular questions please post. 如果您有特殊问题,请发表。

Explanation of what is happening... 发生了什么的解释...

The ~ are delimiters which tell the regex engine where the expression starts at ends. ~是分隔符,用于告诉正则表达式引擎表达式从何处开始。 The m on the outside is a modifier which tells it to treat each line as a as a string. 外部的m是一个修饰符,它告诉它将每行都视为一个字符串。 The ^ and $ tell it to match to start and end of a "string", in this case each line because of the m modifier. ^$告诉它匹配“字符串”的开头和结尾,在这种情况下,每行都由于m修饰符。 The \\ before the { is to escape the curly brace which has a special context in regex. {之前的\\将转义花括号,而后者在正则表达式中具有特殊的上下文。 The . . is any character and the * is a quantifier meaning zero or more occurrences. 是任何字符,而*是表示零次或多次出现的量词。 When these are paired together it means zero or more of any characters. 将这些配对在一起时,表示任何字符为零个或多个。 The () around this are a capturing group which stores what is inside it, and the \\} is so we stop a the last curly brace. 周围的()是一个捕获组,用于存储其中的内容,而\\}是这样,因此我们停止了最后一个花括号。 So from {'/Users/aaron/Applications/developer-vagrant/web/g.php': {'total': 22}} we end up with '/Users/aaron/Applications/developer-vagrant/web/g.php': {'total': 22} . 因此,从{'/Users/aaron/Applications/developer-vagrant/web/g.php': {'total': 22}}我们得到了'/Users/aaron/Applications/developer-vagrant/web/g.php': {'total': 22} We pass this into a function because we want to filter this down further. 我们将其传递给函数,因为我们想进一步过滤掉它。 We use the global here because we are inside of this anonymous function and want it to be accessible when we are done.The '(.*?)' is searching for everything between the single quotes. 我们在这里使用global变量是因为我们位于该匿名函数内部,并且希望完成后可以访问它'(.*?)'正在搜索单引号之间的所有内容。 This is known as a lazy/non greedy, the ? 这就是所谓的“懒惰/非贪婪” ? makes it stop at the first occurrence of the next character (the single quote). 使它停在下一个字符(单引号)的第一个出现处。 The \\s* are any amount of whitespace. \\s*是任意数量的空格。 The rest of the regex here should be decipherable from the previous descriptions. 这里的正则表达式的其余部分应该可以从前面的描述中找到。 The $matches[1] is because we want to first grouped value from the preg_replace_callback the $matches[0] is everything that was found (same with preg_match ). $matches[1]是因为我们要首先将preg_replace_callback的值分组,因此$matches[0]是找到的所有内容(与preg_match相同)。 Then on the final line we assign our global variable the new value. 然后在最后一行,为全局变量分配新值。

I matched both targets using this pattern : /(\\'.*?\\'):\\s?\\{'.*?(\\d{1,})\\}/ 我使用以下模式匹配了两个目标:/( /(\\'.*?\\'):\\s?\\{'.*?(\\d{1,})\\}/ : /(\\'.*?\\'):\\s?\\{'.*?(\\d{1,})\\}/

Explanation : 说明:

  • (\\'.*?\\') - Group 1 : Match any amount of characteres BETWEEN char ''' (lazy) (\\'.*?\\') -组1:在char'''之间匹配任意数量的字符(惰性)
  • :\\s?\\{'.*? - Followed by ':' and O or 1 espace character and char '{' and any amount of any characters (lazy) -后跟':'和O或1个espace字符和char'{'以及任意数量的任何字符(惰性)
  • (\\d{1,})\\} - Group 2 : 1digits at least and followed by '}' (\\d{1,})\\} -第2组:至少1位数字,后跟'}'

See Demo 观看演示

<?php
$array_input = 
     array( 0 => "{'/Users/aaron/Applications/developer-vagrant/web/g.php': {'total': 22}}", 
            1 => "{'/Users/aaron/.vim/autoload/timetap.vim': {'total': 0}}");

$pattern = "/(?:(\'.*?\'):\s?\{'.*?(\d{1,})\})/";
$array_output = array();

for($i = 0; $i < count($array_input); ++$i)
{
    preg_match($pattern, $array_input[$i], $output);
    $array_output[$i][0] = $output[1];
    $array_output[$i][1] = array('total' => ($output[2]));
}

print "<pre>";
print_r($array_output);
print "<pre>";
?>

OUPUT : 输出:

Array
(
[0] => Array
    (
        [0] => '/Users/aaron/Applications/developer-vagrant/web/g.php'
        [1] => Array
            (
                [total] => 22
            )

    )

[1] => Array
    (
        [0] => '/Users/aaron/.vim/autoload/timetap.vim'
        [1] => Array
            (
                [total] => 0
            )

    )

)

That looks like it's already in JSON, so you could just use json_decode() to turn it into objects. 看起来它已经在JSON中,因此您可以使用json_decode()将其转换为对象。 All you need to do to make it compatible with PHP's json_decode() is turn the single ticks into double quotes. 要使其与PHP的json_decode()兼容,您需要做的就是将单笔价格变成双引号。

$string = "{'/Users/aaron/Applications/developer-vagrant/web/g.php': {'total': 22}}";
$string = str_replace("'", '"', $string);
$object = json_decode($string);
var_dump($object);
/*
Outputs the following:
object(stdClass)#1 (1) {
  ["/Users/aaron/Applications/developer-vagrant/web/g.php"]=>
  object(stdClass)#2 (1) {
    ["total"]=>
    int(22)
  }
}
*/

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

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