加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 06:04:24 所属栏目:百科 来源:网络整理
导读:我想从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” 但我不知道如何做到这一点. 我能得到的最近的是:
我想从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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读