vb6 拆分命令行手册中的输出参数

前端之家收集整理的这篇文章主要介绍了vb6 拆分命令行手册中的输出参数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

INGRESS:首节点nodeid

test

EGRESS:末节点nodeid

首末节点不能相同(注:下同)

INDEX:索引

需要按冒号拆分为3个参数。

‘-----------------------------------------------------------------------------------------------------------------------


Attribute VB_Name = "ModCommon"
Option Explicit


'得到 字符串 数组的最大下标
Public Function GetArrayBound_String(ByRef ArrayName() As String) As Long

On Error GoTo ArrayIsEmpty
GetArrayBound_String = UBound(ArrayName)

Exit Function
'--------数组为空--------------------
ArrayIsEmpty:
GetArrayBound_String = -1
End Function
Public Function GetHeadTail(ByVal strSour As String,ByVal strSplit As String _
,ByRef strHead As String,ByRef strTail As String) As Boolean
Dim nStartPos As Long
Dim strReturn As String '返回值

nStartPos = InStr(1,strSour,strSplit)
If nStartPos > 0 Then
strHead = Left(strSour,nStartPos - 1)
strTail = Mid(strSour,nStartPos + Len(strSplit))
GetHeadTail = True
Else
GetHeadTail = False
End If
End Function

‘-----------------------------------------------------------------------------------------------------------------------

Attribute VB_Name = "Module1"

Option Explicit


' 命令行手册中的输出参数
Public Type OUT_PARA_TYPE
name As String ' 参数名
value As String ' 参数值
End Type


'得到 OUT_PARA_TYPE 数组的最大下标
Public Function GetArrayBound_OUT_PARA_TYPE(ByRef ArrayName() As OUT_PARA_TYPE) As Long
On Error GoTo ArrayIsEmpty
GetArrayBound_OUT_PARA_TYPE = UBound(ArrayName)

Exit Function
'--------数组为空--------------------
ArrayIsEmpty:
GetArrayBound_OUT_PARA_TYPE = -1
End Function




'判断一个字符串是否都是由字母或者数字或者下划线组成
Function is_can_as_var_name(lstr As String) As Boolean
If lstr = "" Then Exit Function

Dim lstrlen As Long
Dim i As Long

lstrlen = Len(lstr)

For i = 1 To lstrlen
Dim ch As String
ch = Mid(lstr,i,1)
Dim ascnum As Long
ascnum = Asc(ch)

Dim is_letter As Boolean
is_letter = (ascnum > 64 And ascnum < 91) Or (ascnum > 96 And ascnum < 123)

Dim is_num As Boolean
is_num = (ascnum >= 48 And ascnum <= 57)

If Not (is_letter Or is_num Or (ascnum = 95)) Then
is_can_as_var_name = False
Exit Function
End If
Next
is_can_as_var_name = True
End Function


' 判断一个字符串是否是一个参数名
Private Function is_para_name(ByVal name As String) As Boolean
Dim first_char As String
first_char = Left(name,1)

' 首字符不能为数字
If IsNumeric(first_char) Then
is_para_name = False
Exit Function
End If

is_para_name = is_can_as_var_name(name)
End Function


' 判断一行字符串是否是一个参数的开始
Private Function is_para_begin(ByVal strline As String) As Boolean
Dim npos As Long
Dim name As String

npos = InStr(strline,":")
If npos > 0 Then
name = Left(strline,npos - 1)
End If
is_para_begin = is_para_name(name)
End Function


' 添加一个参数到 obj
Private Sub add_one_para(ByVal str As String,ByRef obj As OUT_PARA_TYPE)
GetHeadTail str,":",obj.name,obj.value
obj.name = Trim(obj.name)
obj.value = Trim(obj.value)

' 去掉回车
obj.name = Replace(obj.name,vbCr,"")
obj.value = Replace(obj.value,"")
End Sub


' 拆分命令行手册中的输出参数
Public Sub split_outpara(ByVal str_in As String,ByRef outarray() As OUT_PARA_TYPE)
Erase outarray

Dim arr() As String
arr = Split(str_in,vbLf)

Dim nBound As Long
nBound = GetArrayBound_String(arr)
Dim i As Long
Dim j As Long

Dim new_bound As Long
new_bound = -1

For i = 0 To nBound
Dim str As String
str = arr(i)
For j = i + 1 To nBound
Dim strline As String
strline = arr(j)

If is_para_begin(strline) Then
new_bound = new_bound + 1
ReDim Preserve outarray(new_bound)
add_one_para str,outarray(new_bound)
str = ""
i = j - 1
Exit For
Else
str = str & vbCrLf & strline
End If

Next j
Next i

If Len(str) > 0 Then
new_bound = new_bound + 1
ReDim Preserve outarray(new_bound)
add_one_para str,outarray(new_bound)
End If

Erase arr
End Sub




Public Sub unittest()
Dim str As String
Dim arr() As OUT_PARA_TYPE

str = "INGRESS: 首节点nodeid"
str = str & vbCrLf & "EGRESS: 末节点nodeid"
str = str & vbCrLf & "INDEX: 索引"

split_outpara str,arr
Dim nBound As Long
nBound = GetArrayBound_OUT_PARA_TYPE(arr)
Debug.Assert nBound = 2
Debug.Assert arr(0).name = "INGRESS"
Debug.Assert arr(0).value = "首节点nodeid"
Debug.Assert arr(1).name = "EGRESS"
Debug.Assert arr(1).value = "末节点nodeid"
Debug.Assert arr(2).name = "INDEX"
Debug.Assert arr(2).value = "索引"


str = "INGRESS: 首节点nodeid"
str = str & vbLf & "EGRESS: 末节点nodeid"
str = str & vbCrLf & "INDEX: 索引"

split_outpara str,arr
nBound = GetArrayBound_OUT_PARA_TYPE(arr)
Debug.Assert nBound = 2
Debug.Assert arr(0).name = "INGRESS"
Debug.Assert arr(0).value = "首节点nodeid"
Debug.Assert arr(1).name = "EGRESS"
Debug.Assert arr(1).value = "末节点nodeid"
Debug.Assert arr(2).name = "INDEX"
Debug.Assert arr(2).value = "索引"

'INGRESS: 首节点nodeid
'test
'EGRESS: 末节点nodeid
'首末节点不能相同(注:下同)
'INDEX: 索引

str = "INGRESS: 首节点nodeid"
str = str & vbCrLf & "test"
str = str & vbCrLf & "EGRESS: 末节点nodeid"
str = str & vbCrLf & "首末节点不能相同(注:下同)"
str = str & vbCrLf & "INDEX: 索引"

split_outpara str,arr
nBound = GetArrayBound_OUT_PARA_TYPE(arr)
Debug.Assert nBound = 2
Debug.Assert arr(0).name = "INGRESS"
'Debug.Assert arr(0).value = "首节点nodeid" & vbCrLf & "test"
Debug.Assert arr(1).name = "EGRESS"
'Debug.Assert arr(1).value = "末节点nodeid" & vbCrLf & "首末节点不能相同(注:下同)"
Debug.Assert arr(2).name = "INDEX"
Debug.Assert arr(2).value = "索引"
End Sub

‘-----------------------------------------------------------------------------------------------------------------------

正则表达式判断版本号
要引用 Microsoft VBScript Regular Expressions 5.5

Function is_valid_version(ByVal ver As String) As Boolean

If m_regEx.Test(ver) Then is_valid_version = True End If End Function Private Sub Form_Load() Set m_regEx = CreateObject("VBScript.RegExp") ' &&建立正则表达式对象 m_regEx.IgnoreCase = True ' &&设置是否区分字符大小写 m_regEx.Global = True ' &&设置全局可用性 ' 设置模式 m_regEx.Pattern = "\d+.\d+.\d+.\d+" Text1.Text = "5.99.10.2" Debug.Assert is_valid_version("5.99.10.2") Debug.Assert Not is_valid_version("5") Debug.Assert Not is_valid_version("5.99") Debug.Assert Not is_valid_version("5.99.10") Debug.Assert Not is_valid_version("5.99.10.B10") End Sub

原文链接:https://www.f2er.com/vb/258201.html

猜你在找的VB相关文章