我有一組數字(15 位),需要獲取第 10-11 位數字的值。我正在使用 RegEx,但到目前為止只選擇了前 9 位數字,不能再進一步了。
^[1-9\][0-9\][0-9\][0-9\][0-9\][0-9\][0-9\][0-9\][0-9\]
示例數字是:
188202143002267.50 (need to get only 002)
188202143-002267.50 (need to get only 002 —- already resolved this)
或者如果有更好的方法來做到這一點?我正在使用 Excel VBA。
uj5u.com熱心網友回復:
使用 regexp.replace 和Mid():
Sub Tester2()
Debug.Print Mid(DigitsOnly("188202143002267.50"), 10, 3)
Debug.Print Mid(DigitsOnly("188202143-002267.50"), 10, 3)
Debug.Print Mid(DigitsOnly("XXX188/202-143-002267.50"), 10, 3)
End Sub
Function DigitsOnly(v)
Static re As Object
If re Is Nothing Then
Set re = CreateObject("vbscript.RegExp")
re.Global = True
re.Pattern = "\D"
End If
DigitsOnly = re.replace(v, "")
End Function
uj5u.com熱心網友回復:
使用Replace和Mid:
Value = "188202143002267.50"
' or:
' Value = "188202143-002267.50"
Digit3 = Mid(Replace(Value, "-", ""), 10, 3)
' Digit3 -> "002"
uj5u.com熱心網友回復:
替代通過 FilterXML()
我的目的不是通過這種替代方法來展示更好或更優雅的方式RegEx,而是指出如何FilterXML在 VBA 中使用(畢竟自 2013 年以后可用)來回傳過濾結果。
由于 XML 的處理仍然被視為有點神秘,我想提供一些提示,以更好地理解 FilterXML 函式的作業原理。
FilterXML是什么:
- 函式回傳特定資料
- 來自(格式良好的)XML 內容 字串(大致相當于具有單獨但區分大小寫的節點名稱的 html 標簽結構)
- 通過使用指定的XPath 運算式 字串(參見 @JvdV 在Extract substrings .. 中的優秀示例)
語法 - 見第 2 節
FILTERXML(xml, xpath)
引數 1:XML 內容 - 見第 1 節
XML 標準允許您自己設計使用定制的標記語言。如果將假定輸入“A123”的每個字符視為節點元素,則可以
- 命名起始DocumentElement例如
r(符號化root)和 - 包含輸入字符的各個節點,例如
i - (并注意正確設定結束標簽
這導致了一個簡單的結構良好的層次結構,如
<r>
<i>A</i>
<i>1</i>
<i>2</i>
<i>3</i>
</r>
請注意,在此示例中轉換為有效的 xml 內容是通過將所需的節點名稱(~ "tags")插入到最終的 xml 內容字串中,將其拆分為單個字符并最終連接在一起來執行的。
引數 2:XPath 運算式 - 請參閱第 2 節
XML 層次結構允許XPath運算式在任何指定的節點和子節點參考內進行搜索。因此,這樣的 XPath 運算式字串可以僅像在發布的問題中一樣隔離和回傳數字
"//i[.*0=0]"
其中//i指示搜索i任何層次結構級別的出現,括號[]括起過濾條件以僅回傳數字,.如果結果為零.*0=0,則通過參考節點內容與零的乘法表示,從而排除除數字以外的其他元素。
與 XMLDoM(檔案物件模型)編碼相比,通過 Application.FilterXML 的 XPath 運算式進行過濾不會回傳節點物件(或定義的節點型別),而只會回傳“分配的”節點內容 值(XMLDoM 中的文本子元素) , 這個特殊問題的 Excel 會自動解釋為數字(這里甚至是:Double!)
*) XMLDoM is a cross-platform and language-independent interface treating the document as a tree structure and allowing programmatic access to the tree
Returned results - see section 3
Results are returned as follows:
- a) in the case of several results as a vertical 2-dim generally variant array (in tabular Excel 2019 as dynamic spill range),
- b) if there is only one result: as a single value,
- c) if there is no result (or an incorrect node query): as an error value (the worksheet function would return
#VALUE!, the late-boundApplication.FilterXML()functionError 2015)
Before transforming these different filtering results to any wanted output (e.g. by transposing or type transformations) you would check them via VarType() - see section 2) in the following code example:
Function GetDigitsOnly()
Example call due to OP: Mid$(DigitsOnly(s), 10, 2) where s represents a string input
Function DigitsOnly(ByVal s As String) As String
If Not Len(s) Then Exit Function ' escape if empty string
'1) Get XML content (as FilterXML's first argument)
'a) make string splitable via vbNullChar delimiter
s = StrConv(s, vbUnicode)
'b) atomize via Split()
Dim tmp
tmp = Split(Left(s, Len(s) - 1), vbNullChar, Len(s) \ 2 1)
'c) get wellformed (html-like) xml content
tmp = "<r><i>" & Join(tmp, "</i><i>") & "</i></r>"
'2) apply FilterXML({content},{XPath}) to get digits only
tmp = Application.FilterXML(tmp, "//i[.*0=0]")
'3) return string result
Select Case VarType(tmp)
Case vbError ' not any digit found
Exit Function
Case vbArray vbVariant ' vertical digits array
DigitsOnly = Join(Application.Transpose(tmp), vbNullString)
Case Else ' single (here e.g.: vbDouble) result
DigitsOnly = tmp
End Select
End Function
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/329460.html
下一篇:Azure服務總線同步發送訊息
