#1
|
|||
|
|||
![]()
Greetings,
I am attempting to retrieve and save the chassis type for a device. The following device dialog is relevant: ICX-6450_3-77#show version | i HW:|System:|System Mode:|Chassis HW: Stackable ICX6450-48 I want to save the text between "HW: " and the end of the line. I wrote the following function: Function ReturnFirstInstanceOfRegExp( strREGEX_TO_FIND, strTEXT_TO_SEARCH ) 'If strREGEX_TO_FIND is not found in strTEXT_TO_SEARCH, function returns empty string Dim MYREGEX, objSEARCHRESULT, retValue retValue = "" set MYREGEX = New RegExp MYREGEX.Pattern = strREGEX_TO_FIND ' Set "Pattern" Property for MYREGE object MYREGEX.IgnoreCase = False ' Set "IgnoreCase" Property for MYREGEX object MYREGEX.Global = True ' Set "Global" Property for MYREGEX object 'MsgBox "HERE MYREGEX.Pattern below:" & vbCr & "xxxx" & vbCr & MYREGEX.Pattern & vbCr & "xxxx" & vbCr & "strTEXT_TO_SEARCH is " & vbCr & "xxxx" & strTEXT_TO_SEARCH & vbCr & "xxxx" Set objSEARCHRESULT = MYREGEX.Execute(strTEXT_TO_SEARCH) ' Execute search. Syntax "object.Execute(string)" where object is always a RegExp object If objSEARCHRESULT.Count = 0 then retValue = "" Else retValue = objSEARCHRESULT(0).SubMatches(0) End If ReturnFirstInstanceOfRegExp = retValue End Function I call this function from another function with the text: strCHASSIS = ReturnFirstInstanceOfRegExp( "HW:\s([A-Za-z0-9-\s]+)", strDIRECT ). Observe the regex does not include any LF or CR characters. Then using MsgBox to inspect the value of strCHASSIS I observe that it contains the text "Stackable ICX6450-48" and two, count 'em, two carriage returns followed by a line feed. (I determined this by sending the variable to a text file, then turning on the 'show all characters' in Notepad++.) How do I get the variable to contain only the required text? Any help is much appreciated! Cheers, st |
#2
|
|||
|
|||
Hi st,
I think your regular expression is the problem. The \s at the end is what is causing the line ending characters to be matched: Quote:
Quote:
What are the possibilities you need to handle? Quote:
If so, will there always be two digits following the hyphen?
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
#3
|
|||
|
|||
Hi Brenda,
Thanks! That makes sense. Talk about learning the hard way! I don't know exactly what all the possibilities will be. So I tried using \r and \n as delimiters for the string, as in HW:\s([A-Za-z0-9-\s]+)\n and HW:\s([A-Za-z0-9-\s]+)\r but that didn't work either. Do you have any ideas? Thanks so much and, as always, Cheers, st |
#4
|
|||
|
|||
Hi st,
Maybe just add a negative range that excludes the line endings: HW:\s([A-Za-z0-9\sA-Za-z0-9-\s][^\r\n]+) In my limited testing, this would match anything alphanumeric on either side of the hyphen (but not line endings).
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
#5
|
|||
|
|||
Hi st,
One of my colleagues suggested this pattern would work best based on the info provided: HW:\s*([^\r\n]+)
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
#6
|
|||
|
|||
![]()
Ah, that is beautiful!
Thanks so much! Cheers, st |
![]() |
Tags |
regular expressions , vb scripting |
Thread Tools | |
Display Modes | Rate This Thread |
|
|