【发布时间】:2018-07-03 18:50:57
【问题描述】:
我正在尝试从字符串中提取广告尺寸。广告尺寸都是设置的标准尺寸。因此,虽然我希望有一个寻找模式的正则表达式,即 IE 3 数字后跟 2 或 3 个数字,但硬编码它也可以工作,因为我们知道大小将是多少。以下是一些广告尺寸的示例:
300x250
728x90
320x50
我能够找到一些我修改过的几乎可以工作的 VBScript,但是因为我正在搜索的字符串不一致,所以在某些情况下它拉得太多了。例如:
您会发现它在每个实例中都没有正确匹配。
我发现的 VB 代码实际上匹配了除广告尺寸之外的所有内容。我对 VBScript 的了解还不够,无法将其反转为仅查找广告尺寸并拉取它们。因此,它会查找所有其他文本并将其删除。
代码如下。有没有办法修复正则表达式,使其只返回广告尺寸?
Function getAdSize(Myrange As Range) As String
Dim regEx As New RegExp
Dim strPattern As String
Dim strInput As String
Dim strReplace As String
Dim strOutput As String
strPattern = "([^300x250|728x90])"
If strPattern <> "" Then
strInput = Myrange.Value
strReplace = ""
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = True
.Pattern = strPattern
End With
If regEx.Test(strInput) Then
getAdSize = regEx.Replace(strInput, strReplace)
Else
getAdSize = "Not matched"
End If
End If
End Function
请注意,数据之前并不总是有下划线,有时它是一个破折号或前后的空格。
【问题讨论】:
-
在您提供的示例数据中,广告尺寸看起来总是被下划线 (
_) 包围。总是这样吗?如果是这样,这将非常容易。 -
不总是,有时是破折号或空格