DeltaRocked Posted June 14, 2013 Posted June 14, 2013 Hello Friends, I am stuck with some regexs , which are used to extract data from .MIB files. these files are used by SNMP trap notifications. Correct Regexs: StringRegExp($Value, '(?i)(?:traps\s)(\d*)(?: })',3) StringRegExp($Value, '(?i)(.*)(?:NOTIFICATION-TYPE\s)',3) Incorrect Regex: $array=StringRegExp($Value, '(?i)description*[\S\s]*?(?:")?.*(?:".*\S\s.*)', 3) From the below mentioned Sample Data, I am interested in extracting the values for Description belonging to traps. The above regex will extract all the descriptions instead of the desired "traps". Sample Data: expandcollapse popupProd_Name MODULE-IDENTITY LAST-UPDATED "201101151711Z" ORGANIZATION "Name" CONTACT-INFO "contact me " DESCRIPTION "MIB file for ProductName" ::= { CompName 2011 } FoundTrp1 NOTIFICATION-TYPE OBJECTS { eventDate, eventTime, NFname } STATUS current DESCRIPTION "Found_Trp" ::= { traps 1 } FoundTrp2 NOTIFICATION-TYPE OBJECTS { eventDate, eventTime, NFname } STATUS current DESCRIPTION "Found_Trp2" ::= { traps 2 } NFname OBJECT-TYPE SYNTAX DisplayString MAX-ACCESS accessible-for-notify STATUS current DESCRIPTION "Description of NFname" ::= { eventVars 11 } Appreciate your help in advance. Regards Deltarocked
FireFox Posted June 14, 2013 Posted June 14, 2013 (edited) Hi,Maybe this :#include <Array.au3> $s = FileRead("t.txt") $a = StringRegExp($s, '(?s)DESCRIPTION\s\r\n\s{8}"(.*?)"', 3) _ArrayDisplay($a)If not, please provide an example of the desired output.Br, FireFox. Edited June 14, 2013 by FireFox
PhoenixXL Posted June 14, 2013 Posted June 14, 2013 (edited) have a look #include <Array.au3> $s = FileRead("file.txt") $a = StringRegExp($s, '(?sm)DESCRIPTION.*?$(.+?$)(.+?$)', 4) ;0th Index : Full Match ;1st Index : Description ;2nd Index : Traps. For $i = 0 To UBound($a) -1 $Temp = $a[$i] _ArrayDisplay($Temp) Next Logic : Capture the next two lines after the word DESCRIPTION Edited June 14, 2013 by PhoenixXL My code: PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners. MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.
DeltaRocked Posted June 15, 2013 Author Posted June 15, 2013 (edited) Gentlemen - going through your replies . will revert back in a few moments. [uPDATE] Firefox: For some strange reasons - your regex works with the provided Sample Data-set - most probably got to do with the "{8}". I presume the additional non-printable chars got converted while pasting the O/P into the Sample-DataSet. Phoenix: I have been trying to understand your regex (Failed miserably) - but it works . However there is one small thing I need from that --- The regex should grab only those results whose second-line contains the word "traps". Please let me know on how its done - will incorporate this knowledge into future regexs. I have been facing problems with multi-line regexs, due to my lack of knowledge. Regards Deltarocked. [uPDATE] Added the check for "{ traps" within the code provided by Phoenix. $array = StringRegExp($Value, '(?ism)description.*?$(.+?$)(.+?$)', 4) For $i = 0 To UBound($array) - 1 $Temp = $array[$i] If StringInStr($Temp[2], '{ traps ') Then $Temp[1] = StringStripWS($Temp[1], 7) ConsoleWrite(StringReplace($Temp[1],'_',' ') & @CRLF) EndIf Next tried a few other things --- but the above regex is the only one which works. Edited June 15, 2013 by DeltaRocked
FireFox Posted June 15, 2013 Posted June 15, 2013 I presume the additional non-printable chars got converted while pasting the O/P into the Sample-DataSet.$a = StringRegExp($s, '(?s)DESCRIPTION\s\r\n(?:.*?)"(.*?)"', 3)
PhoenixXL Posted June 16, 2013 Posted June 16, 2013 (edited) This would do better. #include <Array.au3> $s = FileRead("file.txt") $a = StringRegExp($s, '(?m)DESCRIPTION(.*\n){2}(.*?traps.*)', 4) ;0th Index : Full Match ;1st Index : Description ;2nd Index : Traps. For $i = 0 To UBound($a) -1 $Temp = $a[$i] _ArrayDisplay($Temp) Next Regards Edited June 16, 2013 by PhoenixXL My code: PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners. MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now