Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Notices

Closed Thread
 
Thread Tools Rate Thread Display Modes
  #1  
Old 08-19-2015, 08:57 PM
stevetye stevetye is offline
Registered User
 
Join Date: Apr 2008
Posts: 4
Cool regular expression(s) for carriage return and/or line feed

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  
Old 08-20-2015, 09:22 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
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:
\s
Matches any white space character including space, tab, form-feed, and so on. Equivalent to [ \f\n\r\t\v].

Quote:
HW:\s([A-Za-z0-9-\s]+)
Did you mean for the last - above to be \-?

What are the possibilities you need to handle?

Quote:
HW: Stackable ICX6450-48
Will there always be a hyphen (-)?

If so, will there always be two digits following the hyphen?
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
  #3  
Old 08-20-2015, 09:53 AM
stevetye stevetye is offline
Registered User
 
Join Date: Apr 2008
Posts: 4
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  
Old 08-20-2015, 10:51 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
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  
Old 08-20-2015, 10:57 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
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  
Old 08-20-2015, 11:53 AM
stevetye stevetye is offline
Registered User
 
Join Date: Apr 2008
Posts: 4
Smile

Ah, that is beautiful!

Thanks so much!

Cheers,
st
Closed Thread

Tags
regular expressions , vb scripting

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 07:08 PM.