表中有一个表示政府机关公文的文号字段,内容类似以下
1999年4月9日 应能取出1999
冀[1997]第1号 应能取出1997
冀(1987)第123号 应能取出1987
冀<1998>第234号 应能取出1998
冀干字(92) --应能取出1992
.........现在我要取出这些行中的年份,写到另一个字段中,该怎么写代码呢?
1999年4月9日 应能取出1999
冀[1997]第1号 应能取出1997
冀(1987)第123号 应能取出1987
冀<1998>第234号 应能取出1998
冀干字(92) --应能取出1992
.........现在我要取出这些行中的年份,写到另一个字段中,该怎么写代码呢?
解决方案 »
- vb 如何读取MS word里的内容,并返回如内容所在页码等信息
- listbox怎么清除选中状态?
- C#中转换图片格式问题,.jpg.bmp.gif可以成功转换出科.ico
- activex在网页中调用 网页中加上参数,控件不显示 反之正常显示
- 救助有没有什么好的关于DataReport的书或实例!!!!!!!!!!!
- DATAGRID里处理BIT类型字段的时候能不能显示为CHECK框样式?急!!!!!!!!!!
- 关于字符的问题.......
- 同志们何不转学C#?
- 要在vb中调用一个vc写的dd.dll中的函数 如何获取返回的数组??急急。!!!!
- 请问哪位有 VB下 用WINSOCK TCP/IP 实现点对点文件互传的资料或代码?
- VB如何把Unicode字符转换成ANSI,并正确在TextBox中显示出来?
- vb autocad 为什么选择集删不掉
dim i as long
for i=1900 to 2008
if instr(s,i)>0 then
GetYear=i
exit function
end if
next
end function当然效率可能不是太好......
Private Sub Command1_Click()
Dim strTxt As String
Dim nLen As Integer
Dim i As Integer
Dim arrStr() As String
Dim strType As String
strTxt = Text1.Text
nLen = Len(strTxt)
ReDim arrStr(nLen)
For i = 1 To nLen
arrStr(i) = Mid(strTxt, i, 1) '一个个字符取出来
If IsNumeric(arrStr(i)) Then '判断是不是数字
strType = strType & arrStr(i)
Else
If strType <> "" And (Len(strType) = 2 Or Len(strType) = 4) Then
If Len(strType) = 2 Then
strType = "19" & strType '这个要根据你的要求来决定19或18...
Exit For
ElseIf Len(strType) = 4 Then
If Val(strType) <= 2008 Then '年份2008以前
Exit For
Else
strType = ""
End If
Else
strType = ""
End If
End If
End If
Next i
If i <> (nLen + 1) Then '如相等则没有找到符合条件的年份
Text2.Text = strType
End If
End Sub
'从一个textbox里取出来放到另一个textbox中,你可以改一下改成你需要的
Private Sub Command1_Click()
Dim strTxt As String
Dim nLen As Integer
Dim i As Integer
Dim arrStr() As String
Dim strType As String
strTxt = Text1.Text
nLen = Len(strTxt)
ReDim arrStr(nLen)
For i = 1 To nLen
arrStr(i) = Mid(strTxt, i, 1) '一个个字符取出来
If IsNumeric(arrStr(i)) Then '判断是不是数字
strType = strType & arrStr(i)
Else
If strType <> "" And (Len(strType) = 2 Or Len(strType) = 4) Then
If Len(strType) = 2 Then
strType = "19" & strType '这个要根据你的要求来决定19或18...
Exit For
ElseIf Len(strType) = 4 Then
If Val(strType) <= 2008 Then '年份2008以前
Exit For
Else
strType = ""
End If
Else
strType = ""
End If
End If
End If
Next i
If i <> (nLen + 1) Then '如相等则没有找到符合条件的年份
Text2.Text = strType
End If
End Sub
Set myRegExp = New RegExp
myRegExp.Pattern = "[12]?[09]?[057689]\d"或者:
Dim myRegExp
Set myRegExp = New RegExp
myRegExp.Pattern = "[\n[(<]((\d{2}){1,2})"
最后一个最特殊,可通过位数判断在前面增加“19”格式太乱,示例数据太少,规则不好定,自己看具体情况决定。
Private Sub Command1_Click() Dim regexpObj As New RegExp
regexpObj.Pattern = "\d+"
regexpObj.Global = True
Dim matches, match, ret
s = "1999 年4月9日 " & vbCrLf & "冀[1997]第1号" & vbCrLf & "冀(1987)第123号 " & vbCrLf & "冀 <1998>第234号 " & vbCrLf & "冀干字 (92)"
Set matches = regexpObj.Execute(s)
For Each match In matches
Select Case Len(match.Value)
Case 2
ret = ret & vbCrLf & "年份是: " & "19" & match.Value
Case 4
ret = ret & vbCrLf & "年份是: " & match.Value
End Select
Next
MsgBox ret
End Sub