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

I would like to have a script into which I could insert RegEx's to test them. I've been working with...
#!/usr/bin/perl #<RegTest.pl> Test a Regular Expression use strict; use warnings; $FNAME="system-auth-ifds"; $fpid = open(XFILE, $FLNAME) or die "Search File Not Found: $!\n"; @fRecords=<FNAME>; close FNAME; $rCount = @fRecords; if (@RECORD = grep (/^\s*password\s+requisite\s+(/lib/security/$ISA/)? +pam_cracklib.*lcredit=([0123456789-]+/, @fRecords )) { print "Yes"; } else { print "No"; }
But get Unmatched ( in regex; marked by <-- HERE in m/^\s*password\s+requisite\s+( <-- HERE / at /home/infosec/bin/RegTest.pl line 10. All help much appreciated, thanx

Replies are listed 'Best First'.
Re: Test RegEx
by moritz (Cardinal) on Apr 08, 2010 at 18:00 UTC
    That's because your regex contains an unmatched (, just as the error message says. Either add an ) where appropriate, or escape the ( so that it doesn't have a special meaning.

    See also: perlretut

    Perl 6 - links to (nearly) everything that is Perl 6.
      I have counted, added, subtracted ")" with no change in the error. I am trying to test the RegEx: ^\s*password\s+requisite\s+(/lib/security/$ISA/)?pam_cracklib.*lcredit=(0123456789-+

        You can't (try to) use / as the regex delimiter and (an unquoted) / within the regular expression. Use a different regex delimiter, for example !:

        ... if (@RECORD = grep (m!^\s*password\s+requisite\s+(/lib/security/$ISA/) +?pam_cracklib.*lcredit=([0123456789-]+!, @fRecords )) { ...

        Also see perlop on m//

Re: Test RegEx
by kennethk (Abbot) on Apr 08, 2010 at 18:00 UTC
    As the error states, you have an unmatched parenthesis. Perhaps your intended regular expression was:

    ^\s*password\s+requisite\s+(/lib/security/$ISA/)?pam_cracklib.*lcredit=([0123456789-]+)

    If this doesn't do what you intended, provide some sample input and output. See perlre or perlretut for more info.

      I have counted, added, subtracted ")" with not change in the error. I am trying to test the RegEx: ^\s*password\s+requisite\s+(/lib/security/$ISA/)?pam_cracklib.*lcredit=(0123456789-+
        As I stated, if the code I posted does not meet your spec, post intended input and output. Without that, anything I suggest is just guess work. Note that the posted solution above is a valid regular expression. The entire conditional line would look like:

        if (@RECORD = grep (/^\s*password\s+requisite\s+(/lib/security/$ISA/)?pam_cracklib.*lcredit=([0123456789-]+)/, @fRecords )) {

        Update: I missed the unescaped slashes, as Corion caught below. You either need to escape them or use a different delimiter, such as:

        if (@RECORD = grep (m!^\s*password\s+requisite\s+(/lib/security/$ISA/)?pam_cracklib.*lcredit=([0123456789-]+)!, @fRecords )) {

        As a side note, see how your character class was linkified? That happened because you did not wrap your code in code tags. See Writeup Formatting Tips and/or Markup in the Monastery.

Re: Test RegEx
by planetscape (Chancellor) on Apr 08, 2010 at 20:33 UTC
Re: Test RegEx
by ww (Archbishop) on Apr 08, 2010 at 20:59 UTC
    Addditionally, don't post code that doesn't match your problem statement.

    Long before you got to the problem with the unescaped forward slashes and missing paren, you would have seen numerous messages from strict and warnings -- yet you make no mention of those.

    If you saw and understood them, you should have fixed them before posting, rather than leaving them as wild-geese for the Monks to chase.

    And, casual (and perhaps also "cynical") inspection leads me to wonder if you're trying to test regular expressions or perhaps you're trying to crack passwords; if so, that would be beyond merely abusive. In any case, this script seems a most peculiar methodology for testing regexen, as suggested by your title and bluntly asserted in your narrative.