VBA 中正则表达式组内的正则表达式
Regex within Regex Groups in VBA
我来自这个问题:How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops
发现这个非常有用 link 那里:https://www.experts-exchange.com/articles/1336/Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html
这就是我将在 VBA 中使用的功能。但是,我要解决的正则表达式如下所示:
OR(Q12 = "YES", Q13 = "ABCD", Q2 <> 3)
它永远只是OR
函数
我的 excel 工作表中有 2 列,如下所示:
Table1
ColA ColB
Q1 YES
Q2 2
Q12 YES
Q13 ABCD
所以我想先创建 n 个组:
Group1: Q12 = "YES"
Group2: Q13 = "ABCD"
Group3: Q2 <> 3
然后,我必须通过再次执行 Regex 来处理每个这样的组,这将给我:
Group1-1: Q12
Group1-2: =
Group1-3: YES
然后我会在 VBA 代码中 VLOOKUP(Q12, Table1, 2, FALSE)
。
如果 Group1-2
是 "=",则 =
,否则 <>
Group1-3
这意味着 Group1 = 1
同样对于所有 n 组
TLDR:
输入:
OR(Q12 = "YES", Q13 = "ABCD", Q2 <> 3)
输出:
Group1-1: Q12
Group1-2: =
Group1-3: "YES"
Group2-1: Q13
Group2-2: =
Group2-3: "ABCD"
Group3-1: Q2
Group3-2: <>
Group3-3: 3
非常感谢!
Edit1:嘿,而不是 Hye
根本不需要使用正则表达式 :) 只需删除逗号并用 space:
分隔即可
Sub NoRegex()
Dim str As String, splitted() As String
str = "OR(Q12 = ""YES"", Q13 = ""ABCD"", Q2 <> 3)"
str = Replace(Replace(Replace(Replace(str, ",", ""), "OR", ""), ")", ""), "(", "")
splitted = Split(str, " ")
End Sub
您会将所有块放在一个数组中:)
我来自这个问题:How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops
发现这个非常有用 link 那里:https://www.experts-exchange.com/articles/1336/Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html
这就是我将在 VBA 中使用的功能。但是,我要解决的正则表达式如下所示:
OR(Q12 = "YES", Q13 = "ABCD", Q2 <> 3)
它永远只是OR
函数
我的 excel 工作表中有 2 列,如下所示:
Table1
ColA ColB
Q1 YES
Q2 2
Q12 YES
Q13 ABCD
所以我想先创建 n 个组:
Group1: Q12 = "YES"
Group2: Q13 = "ABCD"
Group3: Q2 <> 3
然后,我必须通过再次执行 Regex 来处理每个这样的组,这将给我:
Group1-1: Q12
Group1-2: =
Group1-3: YES
然后我会在 VBA 代码中 VLOOKUP(Q12, Table1, 2, FALSE)
。
如果 Group1-2
是 "=",则 =
,否则 <>
Group1-3
这意味着 Group1 = 1 同样对于所有 n 组
TLDR:
输入:
OR(Q12 = "YES", Q13 = "ABCD", Q2 <> 3)
输出:
Group1-1: Q12
Group1-2: =
Group1-3: "YES"
Group2-1: Q13
Group2-2: =
Group2-3: "ABCD"
Group3-1: Q2
Group3-2: <>
Group3-3: 3
非常感谢!
Edit1:嘿,而不是 Hye
根本不需要使用正则表达式 :) 只需删除逗号并用 space:
分隔即可Sub NoRegex()
Dim str As String, splitted() As String
str = "OR(Q12 = ""YES"", Q13 = ""ABCD"", Q2 <> 3)"
str = Replace(Replace(Replace(Replace(str, ",", ""), "OR", ""), ")", ""), "(", "")
splitted = Split(str, " ")
End Sub
您会将所有块放在一个数组中:)