Sub test() arr = Array("RawData_LN14838_40C.csv", "RawData_LN14838_60.5C.csv", "RawData_LN14838_20C.csv") For i = 0 To 2 Debug.Print RegExpExtract(CStr(arr(i)), "_(\S+)C") Next End Sub
Public Function RegExpExtract(Text As String, Pattern As String, Optional Item As Integer = 1) As String On Error GoTo ErrHandl Set regex = CreateObject("VBScript.RegExp") regex.Pattern = Pattern regex.Global = True If regex.test(Text) Then Set matches = regex.Execute(Text) RegExpExtract = matches.Item(Item - 1) Exit Function End If ErrHandl: RegExpExtract = CVErr(xlErrValue) End Function
[/vba]
Доброго времени суток.
Никак не могу сообразить подобрать регулярку, прошу помощи
Нужно извлечь число, между двумя разными символами "_" и "С"
Sub test() arr = Array("RawData_LN14838_40C.csv", "RawData_LN14838_60.5C.csv", "RawData_LN14838_20C.csv") For i = 0 To 2 Debug.Print RegExpExtract(CStr(arr(i)), "_(\S+)C") Next End Sub
Public Function RegExpExtract(Text As String, Pattern As String, Optional Item As Integer = 1) As String On Error GoTo ErrHandl Set regex = CreateObject("VBScript.RegExp") regex.Pattern = Pattern regex.Global = True If regex.test(Text) Then Set matches = regex.Execute(Text) RegExpExtract = matches.Item(Item - 1) Exit Function End If ErrHandl: RegExpExtract = CVErr(xlErrValue) End Function
Sub test() arr = Array("RawData_LN14838_40C.csv", "RawData_LN14838_60.5C.csv", "RawData_LN14838_20C.csv") For i = 0 To 2 Debug.Print RegExpExtract(arr(i)) Next End Sub Function RegExpExtract(u) a = InStrRev(u, "C") b = InStrRev(u, "_") RegExpExtract = Mid(u, b + 1, a - b - 1) End Function
[/vba]
[vba]
Код
Sub test() arr = Array("RawData_LN14838_40C.csv", "RawData_LN14838_60.5C.csv", "RawData_LN14838_20C.csv") For i = 0 To 2 Debug.Print RegExpExtract(arr(i)) Next End Sub Function RegExpExtract(u) a = InStrRev(u, "C") b = InStrRev(u, "_") RegExpExtract = Mid(u, b + 1, a - b - 1) End Function
Нужно извлечь число, между двумя разными символами "_" и "С"
UDF [vba]
Код
Function iChislo(cell$) With CreateObject("VBScript.RegExp") .Global = True .MultiLine = True .Pattern = "\d_(.+)(?:C)" If .test(cell) Then iChislo = .Execute(cell)(0).SubMatches(0) Else iChislo = "" End If End With End Function
[/vba]
Цитата
Нужно извлечь число, между двумя разными символами "_" и "С"
UDF [vba]
Код
Function iChislo(cell$) With CreateObject("VBScript.RegExp") .Global = True .MultiLine = True .Pattern = "\d_(.+)(?:C)" If .test(cell) Then iChislo = .Execute(cell)(0).SubMatches(0) Else iChislo = "" End If End With End Function
Можно в регулярных выражениях использовать захватывающие группы в круглых скобках, например так: [vba]
Код
Private Function execute_numbers(what As Range, pattern As String) As Double With CreateObject("Vbscript.Regexp") .Global = False: .MultiLine = False: .Ignorecase = False: .pattern = pattern If .test(what) Then execute_numbers = CDbl(Replace(.Execute(what).Item(0).submatches.Item(0), ".", ",")): Exit Function End With execute_numbers = -1 End Function Sub test() Dim i As Range For Each i In [A1:A3] i.Offset(0, 1) = execute_numbers(i, "\_([0-9.]+)[CС]") Next i End Sub
[/vba] паттерн: [vba]
Код
"\_([0-9.]+)[CС]"
[/vba]
Можно в регулярных выражениях использовать захватывающие группы в круглых скобках, например так: [vba]
Код
Private Function execute_numbers(what As Range, pattern As String) As Double With CreateObject("Vbscript.Regexp") .Global = False: .MultiLine = False: .Ignorecase = False: .pattern = pattern If .test(what) Then execute_numbers = CDbl(Replace(.Execute(what).Item(0).submatches.Item(0), ".", ",")): Exit Function End With execute_numbers = -1 End Function Sub test() Dim i As Range For Each i In [A1:A3] i.Offset(0, 1) = execute_numbers(i, "\_([0-9.]+)[CС]") Next i End Sub