如何解析线并导致数组-卡住 [英] How to parse a line and result in array - stuck

查看:80
本文介绍了如何解析线并导致数组-卡住的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要尝试的操作:

isNumeric(right(trim(contract_id),1))

isNumeric
    right
        trim
            contract_id
        1

isNumeric(right(trim(contract_id),1), bob, george(five(four, two)))

isNumeric
    right
        trim
            contract_id
        1
    bob
    george
        five
            four
            two

所以基本上需要一行trim(var))并将其组成一个数组(array(trim => array(var))。

So basiclly it take a line (let'say trim(var)) and will make an array of it (array(trim => array(var)).

我确实尝试过使用regex和strpos,但没有结果。 ..我需要帮助。谢谢。

I did try with regex and strpos but no result... I need help. Thanks.

推荐答案

首先使用下降解析器将始终为您提供更好的控制。

整个正则表达式解决方案可以使您跳过错误,或者至少让您继续

而不会胡扯。

First off a descent parser will always give you better control.
A whole regex solution might enable you to skip errors or at least let you continue
and not crap out.

您的格式非常简单以及可以进行内部游览至少可以使您$ out $。使用语言递归,您可以重新输入该正则表达式

,使您能够解析核心。

Your format is extremely simple, and an engine that can do internal recursion could at
least get you an outter match. Using language recursion, you could re-enter that regex
enabling you to parse the core.

我不是php专家,但是如果有的话支持正则表达式递归和语言级别 eval()

将能够在源文本中注入一个数组构造。

然后eval字符串来创建带有参数的嵌套数组图像。

I'm no php expert, but if it supports regex recursion and language level eval() you
will be able to inject an array construct into the source text.
Then eval the string to create an nested array image, complete with parameters.

我实际上将您的文本转换为大约12行Perl的数组,但是在出现中断时将其添加到了

中。

I actually converted your text to an array in about 12 lines of Perl, but added to
it when it got interresting.

这里是Perl示例。它笨拙,可读性强。它可能会给您一些启发,可以在php中尝试(如果它可以做这些事情)。就像我说的,我不是PHP专家。

Here is a Perl sample. Its dumbed down so its readable. It might give you some inspiration to try it in php (if it can do these things). Like I said I'm no php expert.

  use Data::Dumper;

  my $str = '
    asdf("asg")
    isNumeric(right(trim(contract_id),1))
    var = \'aqfbasdn\'
    isNumeric(right(trim ( ,contract_id,),-1, j( ) ,"  ", bob, george(five(four, two))))
  ';

  my $func      = '\w+';           # Allowed characters (very watered down)
  my $const     = '[\w*&^+-]+';
  my $wspconst  = '[\w*&^+\s-]+';

  my $GetRx = qr~
    \s*
    (                       # 1 Recursion group
       (?:
           \s* ($func) \s* 
           [(]
              (?:  (?> (?: (?!\s*$func\s*[(] | [)] ) . )+ ) 
                 | (?1)                                         
              )*                                               
           [)]
       )
     )                                                 
  ~xs;

  my $ParseRx = qr~
    (                        # 1 Recursion group
       (?:
           \s* ($func) \s*                                    # 2 Function name
           [(]
           (                                                  # 3 Function core
              (?:  (?> (?: (?!\s*$func\s*[(] | [)] ) . )+ ) 
                 | (?1)                                         
              )*                                               
           )                                                   
           [)]
                                         # OR..other stuff
                                         # Note that this block of |'s is where               
                                         # to put code to parse constants, strings,
                                         # delimeters, etc ... Not much done, but
                                         # here is where that goes.
                                         # -----------------------------------------
         |  \s*["'] ($wspconst) ["']\s*      # 4,5 Variable constants
         | \s* ($const) \s* 
                                         # Lastly, accept empty parameters, if
         | (?<=,)                        # a comma behind us,
         | (?<=^)(?!\s*$)                # or beginning of a new 'core' if actually a paramater.
       )       
     )                                                 
  ~xs;

##
  print "Source string:\n$str\n";
  print "=======================================\n";
  print "Searching string for functions ...\n";
  print "=======================================\n\n";


  while ($str =~ /$GetRx/g) {
      print "------------------\nParsing:\n$1\n\n";
      my $res = parse_func($1);
      print "String to be eval()'ed:\n$res\n\n";

      my $hashref = eval $res.";";
      print "Hash from eval()'ed string:\n", Dumper( $hashref ), "\n\n";
  }

###
  sub parse_func
  {
      my ($core) = @_;
      $core =~ s/$ParseRx/ parse_callback($2, $3, "$4$5") /eg;
      return $core;
  }

  sub parse_callback
  {
      my ($fname, $fbody, $fconst) = @_;
      if (defined $fbody) {
          return "{'$fname'=>[" . (parse_func( $fbody )) . "]}";
      }
      return "'$fconst'"
  }

输出

Source string:

    asdf("asg")
    isNumeric(right(trim(contract_id),1))
    var = 'aqfbasdn'
    isNumeric(right(trim ( ,contract_id,),-1, j( ) ,"  ", bob, george(five(four, two))))

=======================================
Searching string for functions ...
=======================================

------------------
Parsing:
asdf("asg")

String to be eval()'ed:
{'asdf'=>['asg']}

Hash from eval()'ed string:
$VAR1 = {
          'asdf' => [
                      'asg'
                    ]
        };


------------------
Parsing:
isNumeric(right(trim(contract_id),1))

String to be eval()'ed:
{'isNumeric'=>[{'right'=>[{'trim'=>['contract_id']},'1']}]}

Hash from eval()'ed string:
$VAR1 = {
          'isNumeric' => [
                           {
                             'right' => [
                                          {
                                            'trim' => [
                                                        'contract_id'
                                                      ]
                                          },
                                          '1'
                                        ]
                           }
                         ]
        };


------------------
Parsing:
isNumeric(right(trim ( ,contract_id,),-1, j( ) ,"  ", bob, george(five(four, two))))

String to be eval()'ed:
{'isNumeric'=>[{'right'=>[{'trim'=>['' ,'contract_id','']},'-1',{'j'=>[ ]} ,'  ','bob',{'george'=>[{'five'=>['four','two']}]}]}]}

Hash from eval()'ed string:
$VAR1 = {
          'isNumeric' => [
                           {
                             'right' => [
                                          {
                                            'trim' => [
                                                        '',
                                                        'contract_id',
                                                        ''
                                                      ]
                                          },
                                          '-1',
                                          {
                                            'j' => []
                                          },
                                          '  ',
                                          'bob',
                                          {
                                            'george' => [
                                                          {
                                                            'five' => [
                                                                        'four',
                                                                        'two'
                                                                      ]
                                                          }
                                                        ]
                                          }
                                        ]
                           }
                         ]
        };

这篇关于如何解析线并导致数组-卡住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆