簡體   English   中英

具有數學屬性的Excel VBA字符串轉換

[英]Excel VBA String Conversion with Math Properties

我的好奇心激起了我的興趣,使我感到難過。 我遇到一堆字符串,有些帶有數學符號,有些只是字符的情況。 其中一些字符串很長,有些則不是,但是,我想在具有數學符號(特別是+,-)的任何字符串的完整字符串之后添加一個Chr(10),然后繼續讀取該字符串下一個數學符號,並在其前面插入一個Chr(10)。

最終結果應如下所示:

10+20+30+40 50+60+70+80 123a 123 123b 345 123c 123 123d 123 90+100+110+120 123 123 231 123

Converts to: 

10+20+30+40  
50+60+70+80 
123a 123 123b 345 123c 123 123d 123 
90+100+110+120 
123 123 231 123

在方程式沒有空格的地方,普通字母(可以是數字和字母的混合)可以有空格,但是在找到數學符號時會分開。

我一直在網上尋找一些線索,我想我已經非常接近了。 似乎是split,merge或Right(),Left(),然后Len()函數才是答案。

 If Len(SearchString) = "+" Or Len(SearchString) = "-" Then
    SearchString = Left(SearchString, Chr(10))
 End If

但是,這不起作用。 我可能不太了解Len,Left和Right,但我很肯定SearchString是正確的做法。 還值得注意的是,我提供的If / End If語句確實起作用,它不會返回任何錯誤,但不會執行我想要的操作。

這是使用正則表達式的另一種方法。 我使用.Replace方法

Option Explicit
Sub splitter()
    Dim S As String, RE As Object
    Const spat As String = "((?:(?:\w+[+-])+)+\w+)(?=\s|$)|(?:^|\s)((?:\w+\s+)+\w+)(?=\s|$)"
    Dim sRes As Variant

S = "10+20+30+40 50+60+70+80 123a 123 123b 345 123c 123 123d 123 90+100+110+120 123 123 231 123"

Set RE = CreateObject("vbscript.regexp")
With RE
    .Global = True
    .MultiLine = True
    .Pattern = spat
    sRes = .Replace(S, vbLf & "$1$2")
    MsgBox sRes
End With

End Sub

如何應用此方法取決於您的數據設置。 查看原始數據並在某個地方吐出結果。

分離器

((?:(?:\w+[+-])+)+\w+)(?=\s|$)|(?:^|\s)((?:\w+\s+)+\w+)(?=\s|$)

選項:不區分大小寫; ^ $在換行符不匹配

$ 1 $ 2

RegexBuddy創建

大致來說,它可以滿足您的需求。

sentence() = Split(10+20+30+40 50+60+70+80 123a 123 123b 345 123c 123 123d 123 90+100+110+120 123 123 231 123)

 For i = 0 To UBound(sentence)
  acu_str = sentence(i)
   If InStr(sentence(i), "+") = 0 And InStr(sentence(i), "-") = 0 Then
    Do While InStr(sentence(i + 1), "+") = 0 And InStr(sentence(i + 1), "-") = 0
    On Error GoTo ErrorHandling
     acu_str = acu_str + " " + sentence(i + 1)
    i = i + 1
    Loop
   ErrorHandling:
  End If
 Debug.Print acu_str & vbCr
 Next i

因此,這不是理想的方法,但使用正則表達式進行破壞。

Option Explicit
Public Sub TEST()
    Dim tests(), i As Long
    tests = Array("10+20+30+40 50+60+70+80 123a 123 123b 345 123c 123 123d 123 90+100+110+120 123 123 231 123")
    For i = LBound(tests) To UBound(tests)
        Debug.Print ReplaceString(tests(i))
    Next
End Sub

Public Function ReplaceString(ByVal inputString As String) As String
Dim matches As Object, match As Object
  With CreateObject("VBScript.RegExp")
            .Global = True
            .MultiLine = True
            .Pattern = "(\w+\+|\-)+(\w)+"
            If .TEST(inputString) Then
              Set matches = .Execute(inputString)
              For Each match In matches
                  inputString = Replace$(Replace$(inputString, Chr$(32) & match, Chr$(10) & match), match & Chr$(32), match & Chr$(10))
              Next
              ReplaceString = Trim$(inputString)
            Else
              ReplaceString = inputString
            End If
  End With
End Function

第一捕獲組(\\w+\\+|\\-)+

\\+量詞-匹配一次和無限次,盡可能多地匹配,並根據需要返回(貪婪)

第一替代\\w+\\+

\\w+匹配任何單詞字符(等於[a-zA-Z0-9_]

\\+量詞-匹配一次和無限次,盡可能多地匹配,並根據需要返回(貪婪)

\\+逐字匹配字符+ (區分大小寫)

第二個替代項\\- \\-匹配字符-按字面值(區分大小寫)

第二捕獲組(\\w)+

\\+量詞-匹配一次和無限次,盡可能多地匹配,並根據需要返回(貪婪)

\\w匹配任何單詞字符(等於[a-zA-Z0-9_ ])

在這里嘗試

感謝@RonRosenfeld幫助我改善正則表達式。

InStr和InStrRev應該能夠處理拆分。

Option Explicit    

Function splitString(str As String)

    Dim a As Long, b As Long

    a = InStr(1, str, Chr(32))
    b = InStr(1, str, Chr(43))

    Do While a > 0 And b > 0

        If b > a Then
            a = InStrRev(str, Chr(32), b)
        End If

        Mid(str, a, 1) = Chr(10)

        b = InStr(a, str, Chr(43))
        a = InStr(a, str, Chr(32))

    Loop

    splitString = str

End Function

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM