正则expression式来删除一切从一个特定的string开始,其次是什么?
该代码正在工作,但它不会遍历每一行以将答案放在其相邻列中。 所有的列B都得到最后一行的答案。
Private Sub simpleRegex() 'Dim strPattern As String: strPattern = "^[0-9]{1,2}" Dim strPattern As String: strPattern = "BSU:.*" Dim strReplace As String: strReplace = "" Dim regEx As New RegExp Dim strInput As String Dim Myrange As range Dim r As range Set r = ActiveSheet.range("A1", range("A1").End(xlDown)) Set Myrange = ActiveSheet.range("A1:A5") For Each cell In Myrange If strPattern <> "" Then strInput = cell.Value With regEx .Global = True .MultiLine = True .IgnoreCase = False .Pattern = strPattern End With If regEx.Test(strInput) Then r.Offset(0, 1).Value = (regEx.Replace(strInput, strReplace)) 'MsgBox (regEx.Replace(strInput, strReplace)) Else 'MsgBox ("Not matched") End If End If Next End Sub
BSU:*
只适用于前面的字符,所以它会匹配:
0或多次。
匹配一个以BSU:
开头的stringBSU:
然后是任何使用
BSU:.*
在这种情况下.
匹配任何字符和.*
它匹配任何字符0或更多的次数,这是你所需要的,除非你只想匹配什么是BSU:
之后 BSU:
也可以做(向前看或捕获组)。