search字母数字string中的特定字符
我有一串字符,我需要从六个数字之间search字母大写P
,每边三个。 它的位置可以在string中变化,但每次都会有六位数的情况。 P的两边的数字是000到999。
我想找一个公式的字母P的string中的数字位置。
这是一个string示例:(请注意,有几个大写字母P要与之抗衡。)
TCPXX *,CWOP-1:@ 082050z4713.76N / 12228.23W_005 / 005g010t040r000p 000P000 h96b10210L086eWUHU216DAVISVP2。
鉴于你走了VBA的路线,我会使用一个字符循环的字符RegExp。 这直接findP位置。
样本
Sub Test() Debug.Print StripStr("TCPXX*,CWOP-1:@082050z4713.76N/12228.23W_005/005g010t040r000p000P000h96b10210L086eWUHU216DAVISVP2.") Debug.Print StripStr("notme") Debug.Print StripStr("123P456") End Sub
码
Function StripStr(strIn As String) As String Dim objRegex As Object Dim objRegexMC As Object Set objRegex = CreateObject("vbscript.regexp") With objRegex .Pattern = "\d{3}P\d{3}" If .Test(strIn) Then Set objRegexMC = .Execute(strIn) StripStr = objRegexMC(0).firstindex + 4 Else StripStr = "not matched" End If End With End Function
感谢chris neilson,chandoo和brettdj的投入。
一个公式很难build立。
我进行了更深入的search,实际上遇到了这个VBA解决scheme。
http://www.excelfox.com/forum/f22/find-a-text-substring-that-matches-a-given-pattern-331/
像这个应用程序的冠军一样工作。
来自链接的代码:
Function GetPattern(Source As String, ByVal Pattern As String) As String Dim X As Long, FindPattern As Long Do Until Left(Pattern, 1) <> "*" Pattern = Mid(Pattern, 2) Loop For X = 1 To Len(Source) If Mid(Source, X) Like Pattern & "*" Then FindPattern = X Exit For End If Next If FindPattern = 0 Then Exit Function For X = 1 To Len(Source) - FindPattern + 1 If Mid(Source, FindPattern, X) Like Pattern Then GetPattern = Mid(Source, FindPattern, X) Exit For End If Next End Function
你可以通过使用正则expression式来完成 。
看到这篇文章他们解决如何在Excel中使用它们:
- 如何在Microsoft Excel中使用正则expression式(正则expression式)在单元格内和循环中
在你的情况下,你可能想要的expression式:
\d\d\dP\d\d\d
其中匹配3位数字,P和3位数字。