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

if($line =~ /(#include)( ")([^g]\w+)(\.)(\w+")/)
i have written above if condition to find substring starting with any other alphabet but not g . but it is not working proper . is there any other way to do so.

20050317 Janitored by Corion: Added code tags

Replies are listed 'Best First'.
Re: string matching condition not working fine
by Tanktalus (Canon) on Mar 17, 2005 at 18:35 UTC

    Can you tell us what you're getting, and what you're expecting? This seems to work fine for me...

    while (my $line = <DATA>) { chomp $line; if ($line =~ /(#include)( ")([^g]\w+)(\.)(\w+")/) { print "$. MATCH [$line]\n"; } else { print "$. not match [$line]\n" } } __DATA__ not an include #include "foo.h" #include <foo.h> #include "gfoo.h"
    This produces:
    1 not match [not an include] 2 MATCH [#include "foo.h"] 3 not match [#include <foo.h>] 4 not match [#include "gfoo.h"]

Re: string matching condition not working fine
by sh1tn (Priest) on Mar 17, 2005 at 18:51 UTC
    while( <DATA> ){ if( /^\s*(\Winclude\s+\W([^g]\w+\.?\w*)\W)/ ){ print "Match: $1 at line $.$/" } } __DATA__ #include "abc.h" #include "gbc.h" #include <cbc.h> #include <gcg.h> __END__ STDOUT: Match: #include "abc.h" at line 2 Match: #include <cbc.h> at line 4


Re: string matching condition not working fine
by RazorbladeBidet (Friar) on Mar 17, 2005 at 18:33 UTC
    Well, that's going to match anything that starts with #include

    Do you have an example of what you'd like to match and not like to match? More importantly, do you have more detailed requirements? Are some of those supposed to be "optional".

    Your code and your stated (English) requirements do not match - hence there's some confusion.
    --------------
    It's sad that a family can be torn apart by such a such a simple thing as a pack of wild dogs
Re: string matching condition not working fine
by jhourcle (Prior) on Mar 17, 2005 at 19:20 UTC

    ([^g]\w+)

    ...any other alphabet but not g

    Your example actually matches 'any character not g, followed by one or more alphanumeric characters'. You might prefer:

    ([a-fh-zA-FH-Z]\w*)

    (assuming you wanted any alphanumeric characters being allowed in the name) ... if you only wanted alphabetic, try using

    ([a-fh-zA-FH-Z][a-zA-Z]*)