Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hello

I need your help , actually I am trying to find out those words which end with ""(double quotes ) , using grep. so I am doing this . please tell me why is it wrong and how to do it

@ar = grep {/^""$/} @array;

Replies are listed 'Best First'.
Re: grep for ending with double quotes
by davido (Cardinal) on Jun 25, 2013 at 05:09 UTC

    Your question asks to find words (presumably one per array element) that end in two " characters. But your regexp requires that the item being tested starts and ends with "", or in other words, contains "" and only "". You might want this:

    my @ar = grep /""$/, @array;

    Dave

      Hey thanks for helping , I have one more issue that , I have written a code for finding which words begin with " like "asd

       @arr = grep(/"/, @arr_s);

      but it also takes words which end with " like abcd" so how can I ensure it takes only those words which start with " not end with "

        Remember how in my first reply, removing the ^ eliminated the requirement that the match occur at the beginning of the string? Guess what. Putting it back will reinstate that requirement:

        my @arr = grep /^"/, @arr_s;

        One metacharacter at a time, one question at a time is a really inefficient way to learn about regular expressions. Please have a look at perlretut, perlrequick, and perlre. The first two of those documents will take a half hour to 45 minutes to get through, and then you'll be ready to save the day with regular expressions. ;)

        Update: I can see I missed the part of your question that says words shouldn't end with ":

        /^"[^"]$/

        That would accept anything that starts with a ", but contains no " characters after that. Or...

        /^"\p{Alpha}+$/

        In the old days "\w" was marginally useful, but in a Unicode world, \p{Alpha} is probably more accurate. By the way, are you worried about words like "ain't" and "they're"?


        Dave

        ..so how can I ensure it takes only those words which start with " not end with "..
        You ask Perl to do that exactly like so:

        print join "\n" => grep /"\w+?/, qw( abc efgh" "ijk lmn "opqr stuv");
        ..produces...
        "ijk "opqr
        Please, pay close attention to the link provided by Happy-the-monk

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me
Re: grep for ending with double quotes
by Happy-the-monk (Canon) on Jun 25, 2013 at 05:59 UTC