gpt4 book ai didi

regex - 从匹配的正则表达式中提取 Excel 字符串 (VBA)

转载 作者:行者123 更新时间:2023-12-02 09:18:03 33 4
gpt4 key购买 nike

我想从 Excel VBA 中的给定字符串中提取匹配的 RegExp 模式。

例如,

给出这个表达式:

"[0-9]*\+[0-9]{3}\@[0-9]*\+[0-9]{3}"

来自该字符串:“CSDT2_EXC_6+000@6+035_JM_150323”

我想得到:“6+000@6+035”

但我不知道如何实现这一点。

我能得到的最接近的是:

Function getStations(file_name As String)

'Use Regular Expressiosn for grabbing the input and automatically filter it
Dim regEx As New RegExp

With regEx
.Global = True
.MultiLine = True
.IgnoreCase = True
'This matches the pattern: e.g. 06+900@07+230
.Pattern = "[0-9]*\+[0-9]{3}\@[0-9]*\+[0-9]{3}"
End With

If regEx.Test(file_name) Then
strReplace = ""
getStations = regEx.Replace(file_name, strReplace)
Else
getStations = "Hay un problema con el nombre. Por favor, arréglalo"
End If


End Function

但这会给我带来以下结果:“CSDT2_EXC__JM_150323”

我只想采用匹配的模式。我怎样才能实现这个目标?

感谢一百万的回复;)

最佳答案

你可以使用这个:

Function getStations(file_name As String)

'Use Regular Expressiosn for grabbing the input and automatically filter it
Dim regEx As New RegExp

With regEx
.Global = True
.MultiLine = True
.IgnoreCase = True
'This matches the pattern: e.g. 06+900@07+230
.Pattern = "[0-9]*\+[0-9]{3}\@[0-9]*\+[0-9]{3}"
End With

If regEx.Test(file_name) Then
getStations = regEx.Execute(file_name)(0)
Else
getStations = "Hay un problema con el nombre. Por favor, arréglalo"
End If


End Function

关于regex - 从匹配的正则表达式中提取 Excel 字符串 (VBA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29236632/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com