[ale] Single out a alarm with regex

Alex LeDonne aledonne.listmail at gmail.com
Thu Oct 5 09:34:55 EDT 2006


On 10/4/06, Christopher Fowler <cfowler at outpostsentinel.com> wrote:
> On Wed, 2006-10-04 at 16:05 -0400, Alex LeDonne wrote:
> > On 10/4/06, Christopher Fowler <cfowler at outpostsentinel.com> wrote:
> > > I've started with a regex that looks like this:
> > >
> > > '^(DCH:.*?(MAINT).*$)'
> > >
> > > The outer '(' are there so I can capture the whole thing in $1.
> > >
> > > what I'm trying to do is create a regex that will catch anything that
> > > matches this format:
> > >
> > > DCH: 0 <TYPE> ....
> > >
> > > I want th whole line but I do not want it if the type is 'MAINT'.  What
> > > I'm trying to do is get it to pass when the word MAINT is not in the
> > > phrase.  Can someone give me a pointer?
> > >
> >
> > Does the language/regex engine in question support zero-width negative
> > lookahead assertions?
> >
> > '^(DCH: .*? (?!MAINT).*$)'
>
> I'm using standard PERL
>
> [cfowler at shuttle ~]$ echo "DCH: 0  MAINT INDICATION" | perl -le '$_ =
> <STDIN>;print "Yes [$1]" if m/^(DCH:.*(?!MAINT).*$)/'
> Yes [DCH: 0  MAINT INDICATION]
>
>
> >
> > I'm assuming that the space characters are part of the format. Use
> > them to your advantage.

Your test does not include the space characters in the regex, so it's
going to match the same as /^(DCH:.{2,}$)/. Also, the fact that spaces
can appear in the trailing part is significant. Is the field after
DCH: always digits? That could be helpful, too...

aledonne at rumba:~> echo "DCH: 0 MAINT INDICATION" | perl -le '$_ =
<STDIN>; print "Yes [$1]" if m/^(DCH:\s+\d+\s+(?!MAINT).*$)/'
aledonne at rumba:~> echo "DCH: 0 ERROR INDICATION" | perl -le '$_ =
<STDIN>; print "Yes [$1]" if m/^(DCH:\s+\d+\s+(?!MAINT).*$)/'
Yes [DCH: 0 ERROR INDICATION]

The more information you have about the format between DCH: and the
zero-width negative lookahead, the more certain you can be about where
it's anchored.

Also, this regex does assume that the last field in fact _begins_ with
"MAINT"... some version of Jason's two-regex solution is much more
sensible if you want to test the whole thing for  that substring.

-A



More information about the Ale mailing list