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

Im trying to extract some words which is present between two double quotes " " Im able to extract but the issue is if any double quoted words is preceded by a word , it is also getting displayed For eg:- In the word :- "perl"monks i need the word Perl to be extracted but "perl"monks is getting displayed.

The snippet is given below:-

use strict; open FILE, "<searchfile.txt"; my @line = <FILE>; for (@line) { if ($_ =~ m{["](.*?)["]}) { print "$_\n"; } }

P.S:- I dont know whether the below scenario is possible but please give suggestions and ideas:-

When Im extracting a string for eg perl in the above scenario the total no of letters is 4

similarly i want to extract only 4 letters (alphanumeric) in the complete text. Thanks Monks!!!!!!!!

Replies are listed 'Best First'.
Re: Extracting Strings - Special Cases
by choroba (Cardinal) on Jan 07, 2013 at 13:21 UTC
    You are printing $_, i.e. the whole line. You only want to print the matched part, i.e. $1 for the first parentheses.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Extracting Strings - Special Cases
by Athanasius (Archbishop) on Jan 07, 2013 at 13:35 UTC

    Hello rajkk04, and welcome to the Monastery!

    i want to extract only 4 letters (alphanumeric) in the complete text.

    Assuming this means: Match the first alphanumeric word of exactly 4 characters appearing between double quotes:

    #! perl use strict; use warnings; my $s = '"perl"monks'; $s =~ m{ " (\w{4}) " }x; print $1, "\n" if $1;

    Output:

    23:31 >perl 475_SoPW.pl perl 23:31 >

    See “Quantifiers” under perlre#Regular-Expressions.

    Update: If you need to match strictly alphanumeric characters only (no punctuation characters), change \w in the regex to [A-Za-z0-9].

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Extracting Strings - Special Cases
by ansh batra (Friar) on Jan 07, 2013 at 13:18 UTC

    try

    $str="hi\"perl\"monks"; $str =~ /.*\"(.*?)\".*/; print "$1\n";

Re: Extracting Strings - Special Cases
by Anonymous Monk on Jan 07, 2013 at 19:59 UTC