我有一列,其中包含一些像这样的字符串:-
RM125 | RM250 | RMX250 | "RM 125" | "RM 250" |"RMX 250"
DR250 | DR350 | "DR 250" | "DR 350" | DR250S | DR250SE | DR350S | "DR250 S" | "DR250 SE" | "DR350 S"
RMZ250 | "RMZ 250" | "RM-Z 250" | "RMZ-250"
现在我只想提取不在双引号(“”)之间的文本
例如:
RM125 | RM250 | RMX250
DR250 | DR350 | DR250S | DR250SE | DR350S
答案 0 :(得分:1)
编辑感谢@PEH指出,如果我的正则表达式不会将引号中的子字符串放在第一个位置(因此不带|
),则它不会排除。我们必须通过更改以下内容来对初始引用的子字符串进行特殊处理:
.Pattern = "^""[^""]+""\s*\||\|\s*""[^""]+""\s*"
我已经在下面的代码中做到了。
您可以使用在VBA中实现的正则表达式:
Function delQuotedStrings(S As String) As String
With CreateObject("vbscript.regexp")
.Pattern = "^""[^""]+""\s*\||\|\s*""[^""]+""\s*" ' changed from "\|\s*""[^""]+""\s*"
.Global = True
delQuotedStrings = .Replace(S, "")
End With
End Function
例如A1中的数据:
B1: =delQuotedStrings(A1)
OR 可以使用非Regex解决方案:
Option Explicit
Function delQuotedStrings(S As String) As String
Dim v, w, col As Collection, i As Long
Set col = New Collection
For Each v In Split(S, "|")
If Not Left(Trim(v), 1) = """" Then _
col.Add v
Next v
ReDim w(1 To col.Count)
For i = 1 To col.Count
w(i) = col(i)
Next i
delQuotedStrings = Join(w, "|")
End Function
正则表达式的解释:
\|\s*"[^"]+"\s*
\|
\s*
"
[^"]+
"
\s*
由RegexBuddy创建