finding words surround by quotations perl

前端 未结 2 603
星月不相逢
星月不相逢 2020-12-20 00:44

I am reading another perl file line by line and need to find any words or set of words surround by single or double quotations. This is an example of the code I am reading i

相关标签:
2条回答
  • 2020-12-20 00:54

    Maybe you can have more than one "string" to capture per line, one solution could be:

    while(my $line=<STDIN>) {
        while( $line =~ /[\'\"](.*?)[\'\"]/g ) {
            print "matched: '$1'\n";
        }
    }
    

    ie, input:

    #!/usr/bin/env perl
    use strict;
    use warnings;
    
    my $string = 'Hello World!' . 'asdsad';
    print "$string\n";
    

    and executing the code will give you:

    matched: 'Hello World!'
    matched: 'asdsad'
    matched: '$string\n'
    
    0 讨论(0)
  • 2020-12-20 01:02

    Just need to add some capturing parenthesis to your regex, instead of printing the whole line

    use strict;
    use warnings;
    
    while (<DATA>) {
        if(/(["'][^"']*["'])/) {
            print "$1\n";
        }
    }
    
    __DATA__
    #!/usr/bin/env perl
    use strict;
    use warnings;
    
    my $string = 'Hello World!';
    print "$string\n"; 
    

    Note, there are plenty of flaws in your regex though. For example '\'' Won't match properly. Neither will "He said 'boo'". To get closer you'll have to do some balanced parenthesis checking, but there isn't going to be any perfect solution.

    For a solution that is a little closer, you could use the following:

    if(/('(?:(?>[^'\\]+)|\\.)*'|"(?:(?>[^"\\]+)|\\.)*")/) {
    

    That would take care of my above exceptions and also strings like print "how about ' this \" and ' more \n";, but there are still edge cases like the use of qq{} or q{}. Not to mention strings that span more than one line.

    In other words, if your goal is perfect, this project may be outside of the scope of most people's skills, but hopefully the above will be of some help.

    0 讨论(0)
提交回复
热议问题