Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

need helpo regarding regular expression

by praveenchappa (Acolyte)
on Nov 27, 2014 at 07:08 UTC ( #1108533=perlquestion: print w/replies, xml ) Need Help??

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

i had a perl script that traverses throuh the file hirarchy and searches for a particular string and gives the count of files the string is present,the problem is when the search string contains '.' the match is not being done ...please help

$string="hai hello hello.doc yes hai hello.doc present in the file"; $oldstring="hello.doc"; if($string=~ m/\Q$oldstring\E/){ print "\nstring found in ...\n$File::Find::name"; $i++; }

i am taking $oldstring from command line argunments and $string is contents of file i even tried "hello\.doc"but the match is not being done

Replies are listed 'Best First'.
Re: need help regarding regular expression
by Discipulus (Abbot) on Nov 27, 2014 at 07:50 UTC
    if you want to practice with regexes, as i need many many times, you may try this software or, even better the site specifically crafted by the monk above, for instance a, mmh.., The regexed monk.

    I also find very usefull the module that explain regexes: YAPE::Regex::Explain

    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      I also like Debuggex for the visualisation of the regex, which often make it easier to find a mistake.

        thanks Eily i updated my home node (that need a big revision..) with the site you suggested!

        L*
        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: need helpo regarding regular expression
by davido (Cardinal) on Nov 27, 2014 at 08:44 UTC

    When I run exactly the code you posted, I get:

    string found in ...

    So for this to be failing for you, then one of two things must be the case: One, the target string is different. Or two, the contents of $oldstring is different.

    This is why I asked what happens when you add print "<<$oldstring>>\n";. And as you said in a followup post, you get <<hello.doc>>.

    I believe that you are not making this up; that you actually ran your script with print "<<$oldstring>>\n";. So since we've verified that $oldstring contains what we expect, that leaves the other possibility, that $string doesn't contain what you expect. So, what happens when you add this instead?

    print "(($string))\n<<$oldstring>>\n";

    If you add that immediately ahead of your "if(..." statement, we can verify that those two variables contain what you expect them to contain, nothing more, nothing less. Once you've verified that they contain exactly what you expect them to contain, if you still have an issue, then we have something pretty interesting to investigate. ;)

    On the other hand, if they contain something different than what you expected at that crucial point immediately before your "if" statement, then the bug is in code we haven't yet been shown.


    Dave

Re: need helpo regarding regular expression
by davido (Cardinal) on Nov 27, 2014 at 07:31 UTC

    What do you see when you add print "<<$oldstring>>\n"; to your script?


    Dave

Re: need helpo regarding regular expression
by Loops (Curate) on Nov 27, 2014 at 08:36 UTC

    Hi there

    Could you please post minimal code that shows your problem and actually runs even when you include:

    use strict; use warnings;

    at the top of the source. It would also help to show the actual output that the short example code produces when you run it. And finally a brief description of what you expected the output to be instead.

    You've apparently not tried to run the quoted code like that yourself because there are undefined variables included. You might be surprised to find that this code actually works once you clean it up enough to compile on its own and that the problem is somewhere else.

      In fairness to the OP, the code posted does run without strictures and warnings; the undefined variable is irrelevant to the problem, and when warnings aren't enabled, the "print" will just silently concatenate that undefined variable into the output.

      Now it's entirely possible that without strictures and warnings, the OP has misspelled a variable, and that since the misspelling isn't being caught by 'strict', either the target string or the 'pattern' variable doesn't contain what he expects.

      But the code he demonstrated is pretty clearly part of something larger (probably a wanted() routine passed into File::Find). So we don't know that strict and warnings aren't being used in the script.

      And now in fairness to you (Loops): The OP's code snippet doesn't demonstrate the problem, and consequently is next to useless in helping us to help him to diagnose his problem. It would be far more useful to us in our quest to help him had he provided a self-contained compilable code snippet that actually runs and actually demonstrates the problem. I have a feeling, though, that if he took the time to boil this down to a test case that he could demonstrate to us, he himself would discover the source of the problem along the way, and we wouldn't have anything to answer. ;)


      Dave

Re: need helpo regarding regular expression
by praveenchappa (Acolyte) on Nov 27, 2014 at 09:11 UTC

    here is my whole script

    ###################################################################### +########### # this program will search the string in that +# # are present in the whole directory structure. + # # Pass command line arguments as follows---- + # # perl removestring.pl path Searchstring + # ###################################################################### +########### #!/usr/bin/perl use strict; use File::Find; use warnings; my $oldstring; my $newstring; my $path; my $i=0; sub checkArgs() { print $#ARGV+1; die "invalid no of arguments [path oldstring new string]!!" if $#ARGV ++1 !=2 ; for ( @ARGV ) { s/\\n/\n/g; } $oldstring="$ARGV[1]"; print "<<$oldstring>>\n"; $path=$ARGV[0]; print "path: $path ,string:$oldstring(y/n):"; my $ok=<STDIN>; chomp($ok); die "please run the program again with correct arguments:!!" if $ok n +e "y"; find(\&wanted,$path); print "\n\nString Found in $i Files"; } sub wanted { my $flag=0; $flag=1 if !-d && $File::Find::name =~ /.bteq$/; if($flag==1){ my $string=""; open FH1, "< $File::Find::name" or die "Couldn't open file: " +; while (<FH1>){ $string .= $_; } if($string=~ m/\Q$oldstring\E/){ print "\nstring found in ...\n$File::Find::name"; $i++; } } } checkArgs();

    this is my file

    SELECT JSON_col FROM stud_info_small ORDER BY JSON_col.stu_id; SELECT JSON_col FROM stud_info_small WHERE JSON_col.stu_id = "stu_10" ORDER BY JSON_col.stu_id; SELECT JSON_col FROM stud_info_small WHERE JSON_col.qualifications = "Associate Degree" ORDER BY JSON_col.stu_id; ORDER BY stud_doc.stud_id; ORDER BY stud_doc.stud_id;

    arguments-- perl search.pl E:\foldername "stud_doc.su_id"

      Which line of your sample input would you expect to match m/stud_doc\.su_id/?

      The closest thing I can find in your sample data to stud_doc.su_id is two occurrences of stud_doc.stud_id. When I run your script using stud_doc.stud_id on the command line, it works fine.


      Dave

Re: need helpo regarding regular expression
by iep (Novice) on Nov 27, 2014 at 08:25 UTC
    I can not see a problem, your string matches the $oldstring pattern, at least as is in the example.
Re: need helpo regarding regular expression
by praveenchappa (Acolyte) on Nov 27, 2014 at 07:43 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1108533]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2021-05-16 17:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (152 votes). Check out past polls.

    Notices?