Spiral Sites
Spiral Sites

Reputation: 131

Get String with Specific Tags in VB

I have already seen this thread - Get String with Specific Tags in C#

I'm looking for the same to get strings from a string with specific tags, but my tags are in the following format:

[BROWSERLINK]
[REMOVE]
[CURRENT_YEAR]
[SITEURL]
[EMAIL]

If I change the regex simply replacing <> with [] it fails telling me:

parsing "(?[=\[)(.*?)(?=\])" - Unterminated [] set.

I can't change my tags to <> as the string also contains HTML tags that I don't want touched, any ideas please?

Upvotes: 1

Views: 113

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626738

[ and ] are considered special characters in regex. You need to escape them to match as literals:

Dim pattern As String = "(?<=\[)(.*?)(?=\])"

This should work for you, see demo.

Your regex - (?[=[)(.*?)(?=]) - has another issue, too: the [ after (? which ruined the positive look-behind structure. As you see, the ?[ is replaced with (?< in my suggested regex. A correct positive look-behind looks like (?<=...).

Upvotes: 2

Related Questions