Reputation: 13
I have a column which has some strings like these :-
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"
now I want to extract only text which is not between double quotes(" ")
for example:
RM125 | RM250 | RMX250
DR250 | DR350 | DR250S | DR250SE | DR350S
Upvotes: 0
Views: 244
Reputation: 60289
EDIT Thanks to @PEH for pointing out that my regex would not exclude quoted substrings if they were in the first location (and therefore not preceded by the |
). We have to make a special case for an initial quoted substring by changing:
.Pattern = "^""[^""]+""\s*\||\|\s*""[^""]+""\s*"
and I have done so in the code below.
You can use Regular Expressions implemented in 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
eg. Data in A1:
B1: =delQuotedStrings(A1)
OR one could use a non-Regex solution:
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
Explanation of the Regex:
\|\s*"[^"]+"\s*
\|
\s*
"
[^"]+
"
\s*
Created with RegexBuddy
Upvotes: 1