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

It is the second half of the syntax below that is incorrect. I am trying to only consider lines that do not start with the entire phrase "A pdb file". Currently, the syntax I wrote will also exclude any file that starts with "A". How can I avoid this?

if ( /^\w[\s]+\w/ and not /^["A pdb file"]/ )

Replies are listed 'Best First'.
Re: Match entire string, not just first character
by Loops (Curate) on Jul 18, 2013 at 19:16 UTC

    You don't need the quotation marks or the square brackets:

    if ( /^\w[\s]+\w/ and not /^A pdb file/ )

    But my guess is you're also trying to match full words separated by a space with the first pattern, so:

    if ( /^\w+[\s]+\w+/ and not /^A pdb file/ )
      Thank you all so much for your help!!!
Re: Match entire string, not just first character
by rjt (Curate) on Jul 18, 2013 at 19:18 UTC

    Update:I didn't notice my copy/paste malfunction right away. Here's the full answer.

    if ( /^\w[\s]+\w/ and not /^["A pdb file"]/ )

    The problem is, the square brackets create a character class: i.e., any of the characters in between them will match. Remove the square brackets (and perhaps the double quotation marks, if you do not wish to match them), and it should work:

    if ( /^\w[\s]+\w/ and not /^A pdb file/ )

    Your first expression matches a single word character, followed by one or more spaces, followed by another single word character (plus zero or more arbitrary characters after that.) Your intent may have been to match words of any length, in which case you would want to use \w+ instead of just \w.

    You can combine those two expressions:

    #!/usr/bin/env perl use 5.012; use warnings; print for grep { /^(?!A pdb file)\w+\s+\w+/ } <DATA>; __DATA__ A pdb file is on this line This line contains A pdb file A pdb file is indented too far A pdb file, but trailing spaces are here No pdb file, trailing spaces Two words and more Two wordsonly

    Output:

    This line contains A pdb file No pdb file, trailing spaces Two words and more Two wordsonly

    Finally, it would be easier to help you if you provided some sample input and expected output; I'm guessing in most of these cases. Hopefully I got it right! See How do I post a question effectively? for some hints for the future.

Re: Match entire string, not just first character
by wee (Scribe) on Jul 18, 2013 at 21:30 UTC
    If you know that you are always going to not want that exact phrase, why not use substr()? Like so:
    my @list = ('A test line', 'foo bar', 'A pdb file that I have', 'this other thing'); foreach my $item (@list) { if (substr($item, 0, 10) ne 'A pdb file') { print "$item\n"; # Or do whatever... } }
Re: Match entire string, not just first character
by Preceptor (Deacon) on Jul 18, 2013 at 19:18 UTC

    Re-read 'perlre' those square brackets don't mean what you think they mean. You want:

    and not /^\"A pdb file\"/

    Square brackets means 'one character from this set'.

Re: Match entire string, not just first character
by Laurent_R (Canon) on Jul 18, 2013 at 21:16 UTC

    OK, you have been told about square brackets that create a character class. Just a small additional point to clarify. I am just wondering if you really want the double quotes. If you want to match the string with the quotes, then leave them in your regex, but it you added the quotes because you thought that were necessary to quote the string in your program, then removes them, they are not necessary. (I hope this is clear.)

    In brief, /foo/ will match the string foo as well as the string "foo", but the /"foo"/ regex will not match the foo string, but only a string containing "foo".

    And there is no need to escape the double quotes with a backslash.