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

RESOLVED -- Okay, maybe I should have checked my input file a little more carefully... turns out I was pointed at the wrong one and it didn't have any instances of the word I was regexp for... I can't figure out how to regexp for a word with underscore in the middle. I'm looking for do_add and do_delete in a file but using /do_add/, /do.add/, and /do\wadd/ give me nothing. I know I'm missing something obvious but I can't figure it out.
while (<filehandle>) { if (/do_add/) { [some stuff here] } }

Replies are listed 'Best First'.
Re: Regexp for word with underscore in the middle
by GrandFather (Saint) on Aug 18, 2005 at 18:35 UTC

    Your code should have found do_add. This finds do_add or do_delete. Note that case is important. Make the regex /do_(add|delete)/i (note the i) to make it case insensitive.

    use warnings; use strict; while (<DATA>) { if (/do_(add|delete)/) { print "Found: $_"; } }

    Perl is Huffman encoded by design.
Re: Regexp for word with underscore in the middle
by Transient (Hermit) on Aug 18, 2005 at 18:20 UTC
    Can you post your code? That would help - there should be nothing wrong with an underscore in a regexp (AFAIK).
      the relevant code is
      while (<filehandle>) { if (/do_add/) { [some stuff here] } }
      It does not execute the code inside the if... but if I change the regexp to /add/ instead... code inside the if is executed, but on a bunch of lines in addition to the do_add line from the input file. (add appears throughout the file)
        No issues here...
        #!/usr/bin/perl use strict; use warnings; while (<DATA>) { if ( /do_add/ ) { print "Wow! Got do_add - $_"; } else { print "Awwww - no do_add - $_"; } } __DATA__ do_taxes do_things do_add do_wop
        Output:
        C:\Perl>perl testdo.pl Awwww - no do_add - do_taxes Awwww - no do_add - do_things Wow! Got do_add - do_add Awwww - no do_add - do_wop
Re: Regexp for word with underscore in the middle
by ww (Archbishop) on Aug 18, 2005 at 18:33 UTC
    try (from your command line):
        perldoc perlretut

    Your question doesn't tell us enough; the code helps, now that you've posted it but examples of your data would be very helpful too.

    updated after OP posted code