Operating System - HP-UX
1756458 Members
3223 Online
108847 Solutions
New Discussion юеВ

Re: Regarding Regular Expression in Perl

 
Raghu Chikkamenahalli
Frequent Advisor

Regarding Regular Expression in Perl

Hi Experts,

Trying to write a RE in perl for the following patterns,

"OV_Node_Down, Status Alarms: Node Down Root Cause: DEL-DAV Lo0"

"OV_Node_Down, Status Alarms: Node Down Cause: TIR-VAK Lo0"

"OV_Node_Down, Status Alarms: Node Down Problem Cause: BLR-VHS Fa0/0"

"OV_Node_Down, Status Alarms: Node Down interface Cause: VIZ-REG Se0/0"

"OV_Node_Down, Status Alarms: Node Down Root Cause: KOL-RGH BR1/0:1"

"OV_Node_Down, Status Alarms: Node Down Root Cause: BHU-TAN Se0"

"OV_Node_Down, Status Alarms: Node Down Root Cause: KOL-DUR Fa0/0"

Now my conditions is,

Need to check for "Node Down" and it should not contain Lo0, Fa0/0 at the end of the string

Tried the following RE, but it is not working for all strings.

/^.*Node Down.*(^Lo0)|(^Fa0/0)$/

Thanks in advance,

Regards,
Raghu


7 REPLIES 7
H.Merijn Brand (procura
Honored Contributor

Re: Regarding Regular Expression in Perl

if (m{Node_Down} && !m{Lo0, Fa0/0$}) {
...
}

If you insist on a single re

m{Node_Down.*$(?
Enjoy, Have FUN! H.Merijn
Hein van den Heuvel
Honored Contributor

Re: Regarding Regular Expression in Perl

Hmm, Merijn, Neither seems to work for me IF I interpret Raghu's to mean .
Admittedly he did not write that, but his trial solution suggests it.

The ", " is not a magical new operator is it?

The "?
Hein van den Heuvel
Honored Contributor

Re: Regarding Regular Expression in Perl

Argh, the second condition in the double-condition should really be written as:

!m{Lo0$|Fa0/0$}

or

!m{(Lo0|Fa0/0)$}

If you want to see a line which has an "Lo0", but does not end in it. Like:

" Node Down Root Cause: DEL-DAV Lo0 blah"

Hein.




H.Merijn Brand (procura
Honored Contributor

Re: Regarding Regular Expression in Perl

I interpreted the text, not the data. You are right in that he wants two patters.

The zeero-width negative-look-behind only works if both patters are of equal length.

So, the correct solution would then be

if (m{Node_Down} && !m{(?:Lo0|Fa0/0)$}) {
...
}

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Hein van den Heuvel
Honored Contributor

Re: Regarding Regular Expression in Perl

Raghu,

The assigned points suggests that the suggested solutions did not provide an answer what you were looking for.

Care to articulate why not?

Are the double quotes part of the actual data lines? I assumed in my olution they were there for illustrating the input data. Easy enough to accommodate for those, if need be.

For better help you may want to attach a (short) simple TEXT file with actual data lines first, and then a section highlighting the desired, and undesired matches.

Cheers,
Hein.

Raghu Chikkamenahalli
Frequent Advisor

Re: Regarding Regular Expression in Perl

Hello Hein,

first of all thanks a lot for your lightning response and apologies for not giving the requirements clearly.

I was looking for a sigle RE which will match a message containg Node Down" and not conating "Fa0/0 or Lo0".

The catch with the solution
m{Node Down.*\s[^LF][^oa]\d\S*$}

was it is not accepting Lxx for Example:La1, which was supposed match.

in anycase your solution helped me to arrive at the required RE

^.*Node Down.*:\s.*\s(?!Lo0)(?!Fa0\/0).{3,}$

thanks again for you assistance.

Best Regards,
Raghu
H.Merijn Brand (procura
Honored Contributor

Re: Regarding Regular Expression in Perl

m{[^LF][^oa]\d\S*$}

Will match a character other than L or F
Followed by a character other than o or a
followed by a digit
followed by any number of non-whitespace
followed by the end of the string (option new-line allowed)

This will obviously match too much. Way too much

/^.*Node Down.*(^Lo0)|(^Fa0/0)$/

is wrong that does never match, as the syntax is not legal

It would be almost impossible to create what you want in a *legible* single regular expression.

sticking to a combined version is so much safer and easier to maintain

m{Node Down} && !m{(?: Lo0 | Fa0/0 )$}x

is IMHO the only way to go if you want to understand what you did 3 months from now

$ perldoc perlre

Will give you lots of learning stuff

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn