Reputation: 1082
A newbie to regex, I'm trying to skip the first set of brackets [word1]
, and match any remaining text bracketed with the open bracket and closing brace [...}
Text: [word1] This is a [word2]bk{not2} sentence [word3]bk{not3}
Pattern: [^\]]\[.*?\}
So what I want is to match [word2]bk{not2}
and [word3]bk{not3}
, and it works, kind of, but I'm ending up with a leading space on each of the matches. Been playing with this for a couple of days (and doing a lot of reading), but I'm obviously still missing something.
Upvotes: 1
Views: 116
Reputation: 952
[^]]
in your pattern match leading space. That matches any character without ]
.
For example, when text is [word1] This is a X[word2]bk{not2}
,
pattern [^\]]\[.*?\}
matches X[word2]bk{not2}
.
if any open brackets doesn't appear between [wordN}
and {notN}
, you can use:
\[[^\[}]*}
Or, you can also use Submatches
with capturing groups.
Sub test()
Dim objRE As Object
Dim objMatch As Variant
Dim objMatches As Object
Dim strTest As String
strTest = "[word1] This is a [word2]bk{not2} sentence [word3]bk{not3}"
Set objRE = CreateObject("VBScript.RegExp")
With objRE
.Pattern = "[^\]](\[.*?\})"
.Global = True
End With
Set objMatches = objRE.Execute(strTest)
For Each objMatch In objMatches
Debug.Print objMatch.Submatches(0)
Next
Set objMatch = Nothing
Set objMatches = Nothing
Set objRE = Nothing
End Sub
In this sample code, pattern has Parentheses for grouping.
Upvotes: 1