Emulation of lex like functionality in Perl or Python

前端 未结 8 2033
梦毁少年i
梦毁少年i 2021-01-13 23:46

Here\'s the deal. Is there a way to have strings tokenized in a line based on multiple regexes?

One example:

I have to get all href tags, their corresponding

8条回答
  •  醉梦人生
    2021-01-14 00:23

    Also check out pQuery it as a really nice Perlish way of doing this kind of stuff....

    use pQuery;
    
    pQuery( 'http://www.perl.com' )->find( 'a' )->each( 
        sub {
            my $pQ = pQuery( $_ ); 
            say $pQ->text, ' -> ', $pQ->toHtml;
        }
    );
    
    # prints all HTML anchors on www.perl.com
    # =>  link text -> anchor HTML
    

    However if your requirement is beyond HTML/Web then here is the earlier "Hello World!" example in Parse::RecDescent...

    use strict;
    use warnings;
    use Parse::RecDescent;
    
    my $grammar = q{
        alpha : /\w+/
        sep   : /,|\s/
        end   : '!'
        greet : alpha sep alpha end { shift @item; return \@item }
    };
    
    my $parse = Parse::RecDescent->new( $grammar );
    my $hello = "Hello, World!";
    print "$hello -> @{ $parse->greet( $hello ) }";
    
    # => Hello, World! -> Hello , World !
    

    Probably too much of a large hammer to crack this nut ;-)

提交回复
热议问题