问题描述
在本主题中,其思想是对数字进行剥离",并通过RegEx将其除以x
. -> 如何使用Excel正则表达式从字符串中提取广告尺寸
In this topic, the idea is to take "strip" the numerics, divided by a x
through a RegEx. -> How to extract ad sizes from a string with excel regex
因此来自:
uni3uios3_300x250_ASDF.html
我想通过RegEx实现:
I want to achieve through RegEx:
300x250
我设法实现了完全相反的目标,并且我在努力争取完成需要做的事情上花费了一些时间.这是我到目前为止所拥有的:
I have managed to achieve the exact opposite and I am struggling some time to get what needs to be done.This is what I have until now:
Public Function regExSampler(s As String) As String
Dim regEx As Object
Dim inputMatches As Object
Dim regExString As String
Set regEx = CreateObject("VBScript.RegExp")
With regEx
.Pattern = "(([0-9]+)x([0-9]+))"
.IgnoreCase = True
.Global = True
Set inputMatches = .Execute(s)
If regEx.test(s) Then
regExSampler = .Replace(s, vbNullString)
Else
regExSampler = s
End If
End With
End Function
Public Sub TestMe()
Debug.Print regExSampler("uni3uios3_300x250_ASDF.html")
Debug.Print regExSampler("uni3uios3_34300x25_ASDF.html")
Debug.Print regExSampler("uni3uios3_8x4_ASDF.html")
End Sub
如果运行TestMe
,您将得到:
uni3uios3__ASDF.html
uni3uios3__ASDF.html
uni3uios3__ASDF.html
这正是我要通过RegEx剥离的内容.
And this is exactly what I want to strip through RegEx.
推荐答案
将IF
块更改为
If regEx.test(s) Then
regExSampler = InputMatches(0)
Else
regExSampler = s
End If
结果将返回
300x250
34300x25
8x4
这是因为InputMatches
保存了RegEx
执行的结果,该结果保存了要与之匹配的模式.
This is because InputMatches
holds the results of the RegEx
execution, which holds the pattern you were matching against.
这篇关于通过RegEx获取模式的不匹配部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!