in reply to Match entire string, not just first character
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.
|
|---|